-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzerolog_test.go
133 lines (106 loc) · 3.28 KB
/
zerolog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package zerolog
import (
"bytes"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/grpclog"
)
func TestSetNewZeroLogger(t *testing.T) {
GrpcLogSetNewZeroLogger()
}
func TestSetZeroLogger(t *testing.T) {
GrpcLogSetZeroLogger(NewGrpcZeroLogger(zerolog.Logger{}))
}
type TestLogSuite struct {
suite.Suite
out *bytes.Buffer
format string
args []interface{}
logger zerolog.Logger
grpclog GrpcZeroLogger
}
func (s *TestLogSuite) SetupSuite() {
s.format = "Philip%v%v"
s.args = []interface{}{"Was", "Here"}
}
func (s *TestLogSuite) SetupTest() {
s.out = &bytes.Buffer{}
s.logger = zerolog.New(s.out)
s.grpclog = NewGrpcZeroLogger(s.logger)
GrpcLogSetZeroLogger(s.grpclog)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
func TestGrpcZeroLogger(t *testing.T) {
suite.Run(t, new(TestLogSuite))
}
func (s *TestLogSuite) TestError() {
grpclog.Error(s.args...)
s.JSONEq(`{"level":"error","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestErrorf() {
grpclog.Errorf(s.format, s.args...)
s.JSONEq(`{"level":"error","message":"PhilipWasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestErrorln() {
grpclog.Errorln(s.args...)
s.JSONEq(`{"level":"error","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestInfo() {
grpclog.Info(s.args...)
s.JSONEq(`{"level":"info","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestInfof() {
grpclog.Infof(s.format, s.args...)
s.JSONEq(`{"level":"info","message":"PhilipWasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestInfoln() {
grpclog.Infoln(s.args...)
s.JSONEq(`{"level":"info","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestWarning() {
grpclog.Warning(s.args...)
s.JSONEq(`{"level":"warn","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestWarningf() {
grpclog.Warningf(s.format, s.args...)
s.JSONEq(`{"level":"warn","message":"PhilipWasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestWarningln() {
grpclog.Warningln(s.args...)
s.JSONEq(`{"level":"warn","message":"WasHere"}`, s.out.String())
}
// Note: gRPC Logger v2 deprecated Print methods to instead use Info methods.
func (s *TestLogSuite) TestPrint() {
s.grpclog.Print(s.args...)
s.JSONEq(`{"level":"info","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestPrintf() {
s.grpclog.Printf(s.format, s.args...)
s.JSONEq(`{"level":"info","message":"PhilipWasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestPrintln() {
s.grpclog.Println(s.args...)
s.JSONEq(`{"level":"info","message":"WasHere"}`, s.out.String())
}
func (s *TestLogSuite) TestV() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
s.CheckV(0) // Check gRPC Info Logs
zerolog.SetGlobalLevel(zerolog.WarnLevel)
s.CheckV(1) // Check gRPC Warn Logs
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
s.CheckV(2) // Check gRPC Error Logs
zerolog.SetGlobalLevel(zerolog.FatalLevel)
s.CheckV(3) // Check gRPC Fatal Logs
s.PanicsWithValue("unhandled gRPC logger level", func() {
s.grpclog.V(4)
})
}
func (s *TestLogSuite) CheckV(l int) {
for i := 0; i <= l; i++ {
s.Truef(s.grpclog.V(i), "level %v not enabled for %v", i, zerolog.GlobalLevel().String())
}
for i := l + 1; i < 4; i++ {
s.Falsef(s.grpclog.V(i), "level %v enabled for %v", i, zerolog.GlobalLevel().String())
}
}