-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
335 lines (263 loc) · 9.93 KB
/
logger_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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package zapctxd_test
import (
"bytes"
"context"
"errors"
"sync"
"testing"
"github.com/bool64/ctxd"
"github.com/stretchr/testify/assert"
"github.com/swaggest/assertjson"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/bool64/zapctxd"
)
func TestLogger(t *testing.T) {
w := bytes.NewBuffer(nil)
c := zapctxd.New(zapctxd.Config{Level: zap.InfoLevel})
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "bar", 2)
ctx = ctxd.WithLogWriter(ctx, w)
type ctxInt int
// Put some pressure on context.
for i := 0; i < 20; i++ {
ctx = context.WithValue(ctx, ctxInt(i), i)
}
c.Debug(ctx, "hello!", "foo", 1)
assert.Equal(t, ``, w.String())
w.Reset()
c.Info(ctx, "hello!", "foo", 1)
assertjson.Equal(t,
[]byte(`{"level":"info","time":"<ignore-diff>","msg":"hello!","bar":2,"foo":1}`), w.Bytes())
w.Reset()
c.Error(ctx, "hello!", "foo", 1)
assertjson.Equal(t,
[]byte(`{"level":"error","time":"<ignore-diff>","msg":"hello!","bar":2,"foo":1}`), w.Bytes())
ctx = ctxd.WithDebug(ctx)
w.Reset()
c.Debug(ctx, "hello!", "foo", 1)
assertjson.Equal(t,
[]byte(`{"level":"debug","time":"<ignore-diff>","msg":"hello!","bar":2,"foo":1}`), w.Bytes())
ctx = ctxd.WithDebug(ctx)
*w = bytes.Buffer{}
c.Debug(ctx, "hello!",
"foo", 1,
"def", ctxd.DeferredJSON(func() any { return 123 }),
"defstr", ctxd.DeferredJSON(func() any { return "abc" }),
)
assertjson.Equal(t,
[]byte(
`{"level":"debug","time":"<ignore-diff>","msg":"hello!","bar":2,"foo":1,"def": 123,"defstr": "abc"}`),
w.Bytes(),
)
}
func TestLogger_concurrency(t *testing.T) {
logger := zapctxd.New(zapctxd.Config{
StripTime: true,
})
buf := &bytes.Buffer{}
ctx := ctxd.WithLogWriter(context.Background(), buf)
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
logger.Error(ctx, "hello")
}()
}
wg.Wait()
assert.Equal(t,
string(bytes.Repeat([]byte(`{"level":"error","time":"<stripped>","msg":"hello"}`+"\n"), 10)),
buf.String())
}
func TestLogger_Importantw(t *testing.T) {
w := bytes.Buffer{}
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
Output: &w,
})
ctx := context.Background()
c.Info(ctx, "hello!", "foo", 1)
c.Important(ctx, "database wiped", "foo", 1)
assertjson.Equal(t,
[]byte(`{"level":"info","time":"<ignore-diff>","msg":"database wiped","foo":1}`), w.Bytes())
}
func TestLogger_Importantw_dev(t *testing.T) {
w := bytes.Buffer{}
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
DevMode: true,
StripTime: true,
Output: &w,
})
ctx := context.Background()
c.Info(ctx, "hello!", "foo", 1)
c.Important(ctx, "account created", "foo", 1)
assert.Equal(t, "<stripped>\tINFO\tzapctxd/logger_test.go:123\taccount created\t{\"foo\": 1}\n", w.String())
}
func TestLogger_ColoredOutput_dev(t *testing.T) {
w := bytes.Buffer{}
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
DevMode: true,
ColoredOutput: true,
StripTime: true,
Output: &w,
})
ctx := context.Background()
c.Info(ctx, "hello!", "foo", 1)
c.Important(ctx, "account created", "foo", 1)
assert.Equal(t, "<stripped>\t\u001B[34mINFO\u001B[0m\tzapctxd/logger_test.go:142\taccount created\t{\"foo\": 1}\n", w.String())
}
func TestNew_atomic_dev(t *testing.T) {
w := bytes.Buffer{}
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
DevMode: true,
StripTime: true,
Output: &w,
})
ctx := context.Background()
for _, lvl := range []zapcore.Level{zap.ErrorLevel, zap.WarnLevel, zap.InfoLevel, zap.DebugLevel} {
c.SetLevelEnabler(lvl)
c.Debug(ctx, "msg", "lvl", lvl)
c.Info(ctx, "msg", "lvl", lvl)
c.Warn(ctx, "msg", "lvl", lvl)
c.Error(ctx, "msg", "lvl", lvl)
c.Important(ctx, "msg", "lvl", lvl, "important", true)
}
assert.Equal(t, `<stripped> ERROR zapctxd/logger_test.go:165 msg {"lvl": "error"}
<stripped> INFO zapctxd/logger_test.go:166 msg {"lvl": "error", "important": true}
<stripped> WARN zapctxd/logger_test.go:164 msg {"lvl": "warn"}
<stripped> ERROR zapctxd/logger_test.go:165 msg {"lvl": "warn"}
<stripped> INFO zapctxd/logger_test.go:166 msg {"lvl": "warn", "important": true}
<stripped> INFO zapctxd/logger_test.go:163 msg {"lvl": "info"}
<stripped> WARN zapctxd/logger_test.go:164 msg {"lvl": "info"}
<stripped> ERROR zapctxd/logger_test.go:165 msg {"lvl": "info"}
<stripped> INFO zapctxd/logger_test.go:166 msg {"lvl": "info", "important": true}
<stripped> DEBUG zapctxd/logger_test.go:162 msg {"lvl": "debug"}
<stripped> INFO zapctxd/logger_test.go:163 msg {"lvl": "debug"}
<stripped> WARN zapctxd/logger_test.go:164 msg {"lvl": "debug"}
<stripped> ERROR zapctxd/logger_test.go:165 msg {"lvl": "debug"}
<stripped> INFO zapctxd/logger_test.go:166 msg {"lvl": "debug", "important": true}
`, w.String(), w.String())
}
func TestNew_atomic(t *testing.T) {
w := bytes.NewBuffer(nil)
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
StripTime: true,
Output: w,
})
ctx := context.Background()
for _, lvl := range []zapcore.Level{zap.ErrorLevel, zap.WarnLevel, zap.InfoLevel, zap.DebugLevel} {
c.SetLevelEnabler(lvl)
c.Debug(ctx, "msg", "lvl", lvl)
c.Info(ctx, "msg", "lvl", lvl)
c.Warn(ctx, "msg", "lvl", lvl)
c.Error(ctx, "msg", "lvl", lvl)
c.Important(ctx, "msg", "lvl", lvl, "important", true)
}
assert.Equal(t, `{"level":"error","time":"<stripped>","msg":"msg","lvl":"error"}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"error","important":true}
{"level":"warn","time":"<stripped>","msg":"msg","lvl":"warn"}
{"level":"error","time":"<stripped>","msg":"msg","lvl":"warn"}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"warn","important":true}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"info"}
{"level":"warn","time":"<stripped>","msg":"msg","lvl":"info"}
{"level":"error","time":"<stripped>","msg":"msg","lvl":"info"}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"info","important":true}
{"level":"debug","time":"<stripped>","msg":"msg","lvl":"debug"}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"debug"}
{"level":"warn","time":"<stripped>","msg":"msg","lvl":"debug"}
{"level":"error","time":"<stripped>","msg":"msg","lvl":"debug"}
{"level":"info","time":"<stripped>","msg":"msg","lvl":"debug","important":true}
`, w.String(), w.String())
}
func TestLogger_ZapLogger(t *testing.T) {
w := bytes.NewBuffer(nil)
c := zapctxd.New(zapctxd.Config{
Level: zap.ErrorLevel, // Error level does not allow Info messages, but allows Important.
StripTime: true,
Output: w,
})
c.ZapLogger().Error("oops", zap.Error(errors.New("failed")))
assert.Equal(t, `{"level":"error","time":"<stripped>","msg":"oops","error":"failed"}`+"\n", w.String())
}
func TestLogger_SetLevelEnabler_FailedToHandleZapLoggers(t *testing.T) {
t.Parallel()
w := bytes.NewBuffer(nil)
enc := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
sl := zap.New(zapcore.NewCore(enc, zapcore.AddSync(w), zapcore.InfoLevel))
l := zapctxd.WrapZapLoggers(sl, sl, enc)
assert.Panics(t, func() {
l.SetLevelEnabler(zapcore.DebugLevel)
})
}
func TestLogger_SetLevelEnabler_Success(t *testing.T) {
t.Parallel()
w := bytes.NewBuffer(nil)
l := zapctxd.New(zapctxd.Config{
Level: zap.InfoLevel,
StripTime: true,
DevMode: true,
Output: w,
})
assert.NotPanics(t, func() {
l.SetLevelEnabler(zapcore.DebugLevel)
})
}
func TestLogger_SkipCaller(t *testing.T) {
w := bytes.NewBuffer(nil)
c := zapctxd.New(zapctxd.Config{
Level: zap.InfoLevel,
StripTime: true,
DevMode: true,
Output: w,
})
do := func() {
c.Info(context.Background(), "hello", "k", "v")
c.SkipCaller().Info(context.Background(), "world", "k", "v")
c.Info(context.Background(), "hello", "k", "v") // Original logger is not affected.
}
do()
assert.Equal(t, `<stripped> INFO zapctxd/logger_test.go:279 hello {"k": "v"}
<stripped> INFO zapctxd/logger_test.go:284 world {"k": "v"}
<stripped> INFO zapctxd/logger_test.go:281 hello {"k": "v"}
`, w.String())
assert.NotNil(t, zapctxd.New(zapctxd.Config{}).SkipCaller())
}
func TestNew_zapOptions(t *testing.T) {
w := bytes.NewBuffer(nil)
c := zapctxd.New(zapctxd.Config{
Level: zap.InfoLevel,
StripTime: true,
Output: w,
ZapOptions: []zap.Option{zap.Fields(zap.String("config", "foo"))},
}, zap.Fields(zap.String("constructor", "bar")))
c.Info(context.Background(), "hello", "k", "v")
assert.Equal(t, `{"level":"info","time":"<stripped>","msg":"hello","config":"foo","constructor":"bar","k":"v"}
`, w.String())
}
func TestNew_structuredError(t *testing.T) {
w := bytes.Buffer{}
c := zapctxd.New(zapctxd.Config{
Level: zap.DebugLevel,
Output: &w,
StripTime: true,
})
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "ctx", 123)
err := ctxd.WrapError(ctx, errors.New("failed"), "making foo", "detail1", 1, "detail2", 2)
c.Debug(ctx, "hello!", "foo", 1, "error", err)
c.Info(ctx, "hello!", "foo", 1, "error", err)
c.Warn(ctx, "hello!", "foo", 1, "error", err)
c.Error(ctx, "hello!", "foo", 1, "error", err)
c.Important(ctx, "hello!", "foo", 1, "error", err)
assert.Equal(t, `{"level":"debug","time":"<stripped>","msg":"hello!","foo":1,"error":"making foo: failed","ctx":123,"detail1":1,"detail2":2}
{"level":"info","time":"<stripped>","msg":"hello!","foo":1,"error":"making foo: failed","ctx":123,"detail1":1,"detail2":2}
{"level":"warn","time":"<stripped>","msg":"hello!","foo":1,"error":"making foo: failed","ctx":123,"detail1":1,"detail2":2}
{"level":"error","time":"<stripped>","msg":"hello!","foo":1,"error":"making foo: failed","ctx":123,"detail1":1,"detail2":2}
{"level":"info","time":"<stripped>","msg":"hello!","foo":1,"error":"making foo: failed","ctx":123,"detail1":1,"detail2":2}
`, w.String(), w.String())
}