-
Notifications
You must be signed in to change notification settings - Fork 3
/
simplog_test.go
312 lines (273 loc) · 7.51 KB
/
simplog_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package simplog
import (
"bytes"
"context"
"net/url"
"strings"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestNewLogger(t *testing.T) {
t.Parallel()
type args struct {
options *Options
}
cases := []struct {
name string
args args
}{
{
name: "nil options",
args: args{
options: nil,
},
},
{
name: "empty options",
args: args{
options: &Options{},
},
},
{
name: "with options; client in debug mode",
args: args{
options: &Options{
Debug: true,
IsServer: false,
},
},
},
{
name: "with options; client in production mode",
args: args{
options: &Options{
Debug: false,
IsServer: false,
},
},
},
{
name: "with options; server in debug mode",
args: args{
options: &Options{
Debug: true,
IsServer: true,
},
},
},
{
name: "with options; server in production mode",
args: args{
options: &Options{
Debug: false,
IsServer: true,
},
},
},
{
name: "with options; client with stacktrace",
args: args{
options: &Options{
IsServer: false,
DisableStacktrace: false,
},
},
},
{
name: "with options; client without stacktrace",
args: args{
options: &Options{
IsServer: false,
DisableStacktrace: true,
},
},
},
}
for _, tc := range cases {
var logger *zap.SugaredLogger
// Doing this to cover all the different constructors.
if tc.args.options == nil {
logger = NewWithOptions(tc.args.options) // Can handle nil options.
tc.args.options = defaultOptions // Doing this to avoid nil pointer dereference in the comparisons below.
} else {
if !tc.args.options.IsServer {
logger = NewClientLogger(tc.args.options.Debug)
} else {
logger = NewServerLogger(tc.args.options.Debug)
}
}
if logger == nil {
t.Fatal("Returned a nil logger")
}
if tc.args.options.Debug {
if !logger.Desugar().Core().Enabled(zap.DebugLevel) {
t.Error("Debug logger not enabled at debug level")
}
} else {
if !logger.Desugar().Core().Enabled(zap.InfoLevel) {
t.Error("Production logger not enabled at info level")
}
}
}
}
func TestFromContext(t *testing.T) {
t.Parallel()
// Positive case; context has a logger.
l1 := NewWithOptions(nil)
if l1 == nil {
t.Fatal("NewClientLogger returned a nil logger")
}
ctx := WithLogger(context.Background(), l1)
if ctx == nil {
t.Fatal("WithLogger returned a nil context")
}
l2 := FromContext(ctx)
if l2 == nil {
t.Fatal("FromContext returned a nil logger")
}
// Comparing memory addresses should be sufficient to determine equality.
if l1 != l2 {
t.Error("FromContext returned a different logger")
}
// Context has no logger, returned logger should still not be nil.
ctx = context.Background()
l3 := FromContext(ctx)
if l3 == nil {
t.Fatal("FromContext returned a nil logger")
}
}
// This is for testing purposes only. Copied from:
// - https://github.com/uber-go/zap/blob/v1.22.0/sink.go#L61
type nopCloserSink struct{ zapcore.WriteSyncer }
func (nopCloserSink) Close() error { return nil }
func TestVisualLevelEncoder(t *testing.T) {
t.Parallel()
// Custom memory sink to capture output.
buf := bytes.NewBuffer(nil)
memFactory := func(u *url.URL) (zap.Sink, error) {
return nopCloserSink{zapcore.AddSync(buf)}, nil
}
err := zap.RegisterSink("mem", memFactory)
if err != nil {
t.Fatalf("register new zap sink: %v", zap.Error(err))
}
// Create a logger with the custom sink and the visualLevelEncoder that we want to test.
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = visualLevelEncoder
config.OutputPaths = []string{"mem://"}
logger, err := config.Build()
if err != nil {
t.Fatalf("build logger: %v", err)
}
// Stopping fatal from exiting the test. We need to recover from the panic, zapcore.WriteThenNoop is not supported
// by fatal.
logger = logger.WithOptions(zap.WithFatalHook(zapcore.WriteThenPanic))
// Use the following function to test panicking logs.
logWithRecovery := func(log func(string, ...zapcore.Field), msg string, fields ...zapcore.Field) {
defer func() {
if r := recover(); r == nil {
t.Fatal("expected panic")
}
}()
log("test")
}
// Log a message at each level and check the buffer for the expected output.
// Debug
logger.Debug("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.DebugLevel]) {
t.Fatal("expected debug symbol in log message")
}
buf.Reset()
// Info
logger.Info("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.InfoLevel]) {
t.Fatal("expected info symbol in log message")
}
buf.Reset()
// Warning
logger.Warn("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.WarnLevel]) {
t.Fatal("expected warn symbol in log message")
}
buf.Reset()
// Error
logger.Error("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.ErrorLevel]) {
t.Fatal("expected error symbol in log message")
}
buf.Reset()
logWithRecovery(logger.Fatal, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.FatalLevel]) {
t.Fatal("expected fatal symbol in log message")
}
buf.Reset()
// Panic
logWithRecovery(logger.Panic, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.PanicLevel]) {
t.Fatal("expected panic symbol in log message")
}
buf.Reset()
// DPanic
logWithRecovery(logger.DPanic, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.DPanicLevel]) {
t.Fatal("expected dpanic symbol in log message")
}
buf.Reset()
// Now repeat the process but with custom symbols.
// Debug
debugSymbol := "<!!D!!>"
SetDebugSymbol(debugSymbol)
logger.Debug("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.DebugLevel]) {
t.Fatalf("expected debug symbol %q in log message: %s", debugSymbol, buf.String())
}
buf.Reset()
// Info
infoSymbol := "<!!I!!>"
SetInfoSymbol(infoSymbol)
logger.Info("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.InfoLevel]) {
t.Fatalf("expected info symbol %q in log message: %s", infoSymbol, buf.String())
}
buf.Reset()
// Warning
warnSymbol := "<!!W!!>"
SetWarnSymbol(warnSymbol)
logger.Warn("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.WarnLevel]) {
t.Fatalf("expected warn symbol %q in log message: %s", warnSymbol, buf.String())
}
buf.Reset()
// Error
errorSymbol := "<!!E!!>"
SetErrorSymbol(errorSymbol)
logger.Error("test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.ErrorLevel]) {
t.Fatalf("expected error symbol %q in log message: %s", errorSymbol, buf.String())
}
buf.Reset()
// Fatal
fatalSymbol := "<!!F!!>"
SetFatalSymbol(fatalSymbol)
logWithRecovery(logger.Fatal, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.FatalLevel]) {
t.Fatalf("expected fatal symbol %q in log message: %s", fatalSymbol, buf.String())
}
buf.Reset()
// Panic
panicSymbol := "<!!P!!>"
SetPanicSymbol(panicSymbol)
logWithRecovery(logger.Panic, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.PanicLevel]) {
t.Fatalf("expected panic symbol %q in log message: %s", panicSymbol, buf.String())
}
buf.Reset()
// DPanic
dpanicSymbol := "<!!DP!!>"
SetDPanicSymbol(dpanicSymbol)
logWithRecovery(logger.DPanic, "test")
if !strings.Contains(buf.String(), activeSymbols.fromZapLevel[zap.DPanicLevel]) {
t.Fatalf("expected dpanic symbol %q in log message: %s", dpanicSymbol, buf.String())
}
}