forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaplogger_test.go
328 lines (287 loc) · 7.95 KB
/
zaplogger_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
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package log_test
import (
"errors"
"fmt"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/plugin"
)
func TestNewZapLog(t *testing.T) {
logger := log.NewZapLog(defaultConfig)
assert.NotNil(t, logger)
logger.SetLevel("0", log.LevelInfo)
lvl := logger.GetLevel("0")
assert.Equal(t, lvl, log.LevelInfo)
l := logger.With(log.Field{Key: "test", Value: "a"})
l.SetLevel("output", log.LevelDebug)
assert.Equal(t, log.LevelDebug, l.GetLevel("output"))
}
func TestNewZapLog_WriteMode(t *testing.T) {
logDir := t.TempDir()
t.Run("invalid write mode", func(t *testing.T) {
const invalidWriteMode = 4
require.Panics(t, func() {
log.NewZapLog([]log.OutputConfig{{
Writer: log.OutputFile,
WriteConfig: log.WriteConfig{
LogPath: logDir,
Filename: "trpc.log",
WriteMode: invalidWriteMode,
},
}})
})
})
t.Run("valid write mode", func(t *testing.T) {
const (
syncFileName = "trpc.syncLog"
asyncFileName = "trpc.asyncLog"
fastFileName = "trpc.fastLog"
)
tests := []struct {
name string
config log.OutputConfig
}{
{"sync", log.OutputConfig{
Writer: log.OutputFile,
WriteConfig: log.WriteConfig{
LogPath: logDir,
Filename: syncFileName,
WriteMode: log.WriteSync,
},
}},
{"async", log.OutputConfig{
Writer: log.OutputFile,
WriteConfig: log.WriteConfig{
LogPath: logDir,
Filename: asyncFileName,
WriteMode: log.WriteAsync,
},
}},
{"fast", log.OutputConfig{
Writer: log.OutputFile,
WriteConfig: log.WriteConfig{
LogPath: logDir,
Filename: fastFileName,
WriteMode: log.WriteFast,
},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NotNil(t, log.NewZapLog([]log.OutputConfig{tt.config}))
})
}
})
}
func TestZapLogWithLevel(t *testing.T) {
logger := log.NewZapLog(defaultConfig)
assert.NotNil(t, logger)
l := logger.With(log.Field{Key: "test", Value: "a"})
l.SetLevel("0", log.LevelFatal)
assert.Equal(t, log.LevelFatal, l.GetLevel("0"))
l = l.With(log.Field{Key: "key1", Value: "val1"})
l.SetLevel("0", log.LevelError)
assert.Equal(t, log.LevelError, l.GetLevel("0"))
}
func BenchmarkDefaultTimeFormat(b *testing.B) {
t := time.Now()
for i := 0; i < b.N; i++ {
log.DefaultTimeFormat(t)
}
}
func BenchmarkCustomTimeFormat(b *testing.B) {
t := time.Now()
for i := 0; i < b.N; i++ {
log.CustomTimeFormat(t, "2006-01-02 15:04:05.000")
}
}
func TestCustomTimeFormat(t *testing.T) {
date := time.Date(2006, 1, 2, 15, 4, 5, 0, time.Local)
dateStr := log.CustomTimeFormat(date, "2006-01-02 15:04:05.000")
assert.Equal(t, dateStr, "2006-01-02 15:04:05.000")
}
func TestDefaultTimeFormat(t *testing.T) {
date := time.Date(2006, 1, 2, 15, 4, 5, 0, time.Local)
dateStr := string(log.DefaultTimeFormat(date))
assert.Equal(t, dateStr, "2006-01-02 15:04:05.000")
}
func TestGetLogEncoderKey(t *testing.T) {
tests := []struct {
name string
defKey string
key string
want string
}{
{"custom", "T", "Time", "Time"},
{"default", "T", "", "T"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := log.GetLogEncoderKey(tt.defKey, tt.key); got != tt.want {
assert.Equal(t, got, tt.want)
}
})
}
}
func TestNewTimeEncoder(t *testing.T) {
encoder := log.NewTimeEncoder("")
assert.NotNil(t, encoder)
encoder = log.NewTimeEncoder("2006-01-02 15:04:05")
assert.NotNil(t, encoder)
tests := []struct {
name string
fmt string
}{
{"seconds timestamp", "seconds"},
{"milliseconds timestamp", "milliseconds"},
{"nanoseconds timestamp", "nanoseconds"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := log.NewTimeEncoder(tt.fmt)
assert.NotNil(t, got)
})
}
}
func TestWithFields(t *testing.T) {
// register Writer.
// use zap observer to support test.
core, ob := observer.New(zap.InfoLevel)
log.RegisterWriter(observewriter, &observeWriter{core: core})
// config is configuration.
cfg := []log.OutputConfig{
{
Writer: observewriter,
},
}
// create a zap logger.
zl := log.NewZapLog(cfg)
assert.NotNil(t, zl)
// test With.
field := log.Field{Key: "abc", Value: int32(123)}
logger := zl.With(field)
assert.NotNil(t, logger)
log.SetLogger(logger)
log.Warn("with fields warning")
assert.Equal(t, 1, ob.Len())
entry := ob.All()[0]
assert.Equal(t, zap.WarnLevel, entry.Level)
assert.Equal(t, "with fields warning", entry.Message)
assert.Equal(t, []zapcore.Field{{Key: "abc", Type: zapcore.Int32Type, Integer: 123}}, entry.Context)
}
func TestOptionLogger2(t *testing.T) {
t.Run("test option logger add caller skip", func(t *testing.T) {
core, ob := observer.New(zap.InfoLevel)
log.RegisterWriter(observewriter, &observeWriter{core: core})
cfg := []log.OutputConfig{{Writer: observewriter}}
l := log.NewZapLogWithCallerSkip(cfg, 1)
l.Info("this is option logger test, the current caller skip is correct")
_, file, _, ok := runtime.Caller(0)
require.True(t, ok)
require.Equal(t, file, ob.All()[0].Caller.File)
ol, ok := l.(log.OptionLogger)
require.True(t, ok)
l = ol.WithOptions(log.WithAdditionalCallerSkip(1))
l.Info("this is option logger test, the current caller skip is incorrect(added 1)")
_, file, _, ok = runtime.Caller(1)
require.True(t, ok)
require.Equal(t, file, ob.All()[1].Caller.File)
})
t.Run("test option logger wrapper add caller skip", func(t *testing.T) {
core, ob := observer.New(zap.InfoLevel)
log.RegisterWriter(observewriter, &observeWriter{core: core})
cfg := []log.OutputConfig{{Writer: observewriter}}
l := log.NewZapLogWithCallerSkip(cfg, 1)
l = l.With(log.Field{Key: "k", Value: "v"})
l.Info("this is option logger wrapper test, the current caller skip is correct")
_, file, _, ok := runtime.Caller(0)
require.True(t, ok)
require.Equal(t, file, ob.All()[0].Caller.File)
ol, ok := l.(log.OptionLogger)
require.True(t, ok)
l = ol.WithOptions(log.WithAdditionalCallerSkip(1))
l.Info("this is option logger wrapper test, the current caller skip is incorrect(added 1)")
_, file, _, ok = runtime.Caller(1)
require.True(t, ok)
require.Equal(t, file, ob.All()[1].Caller.File)
})
}
const observewriter = "observewriter"
type observeWriter struct {
core zapcore.Core
}
func (f *observeWriter) Type() string { return "log" }
func (f *observeWriter) Setup(name string, dec plugin.Decoder) error {
if dec == nil {
return errors.New("empty decoder")
}
decoder, ok := dec.(*log.Decoder)
if !ok {
return errors.New("invalid decoder")
}
decoder.Core = f.core
decoder.ZapLevel = zap.NewAtomicLevel()
return nil
}
func TestLogLevel(t *testing.T) {
config := []log.OutputConfig{
{
Writer: "console",
Level: "",
},
{
Writer: "console",
Level: "trace",
},
{
Writer: "console",
Level: "debug",
},
{
Writer: "console",
Level: "info",
},
{
Writer: "console",
Level: "warn",
},
{
Writer: "console",
Level: "error",
},
{
Writer: "console",
Level: "fatal",
},
}
l := log.NewZapLog(config)
var (
got []string
want []string
)
for i, c := range config {
got = append(got, log.LevelStrings[l.GetLevel(fmt.Sprint(i))])
want = append(want, log.Levels[c.Level].String())
}
require.Equal(t, want, got)
}
func TestLogEnableColor(t *testing.T) {
cfg := []log.OutputConfig{{Writer: "console", Level: "trace", EnableColor: true}}
l := log.NewZapLog(cfg)
l.Trace("hello")
l.Debug("hello")
l.Info("hello")
l.Warn("hello")
l.Error("hello")
}