-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
382 lines (348 loc) · 9.79 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package glog
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewLogger(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
assert.NotNil(t, logger, "Expected new logger to be created")
assert.Equal(t, core, logger.core, "Expected core to be set")
}
func TestLogger_clone(t *testing.T) {
h1 := func(ctx context.Context, record *Record) {}
l1 := &Logger{contextHandlers: []ContextHandler{h1}}
l2 := l1.clone()
assert.NotEqual(t, l1.contextHandlers, l2.contextHandlers, "Expected contextHandlers to be different")
assert.NotEqual(t, l1, l2, "Expected l1 and l2 to be different")
assert.Equal(t, len(l2.contextHandlers), 1, "Expected contextHandlers to be cloned")
}
func TestLogger_formatMessage(t *testing.T) {
type myLogger struct {
core Core
name string
addCaller bool
stackLevel LevelEnabler
callerSkip int
formatEnabled bool
contextHandlers []ContextHandler
}
type args struct {
msg string
args []any
}
tests := []struct {
name string
logger myLogger
args args
wantMessage string
wantFormatted bool
}{
{
name: "formatEnabled is false, no args",
logger: myLogger{},
args: args{
msg: "msg1",
},
wantMessage: "msg1",
wantFormatted: false,
},
{
name: "formatEnabled is false, with args",
logger: myLogger{},
args: args{
msg: "msg is %s",
args: []any{"test"},
},
wantMessage: "msg is %s",
wantFormatted: false,
},
{
name: "formatEnabled is true, no args",
logger: myLogger{formatEnabled: true},
args: args{
msg: "msg1",
},
wantMessage: "msg1",
wantFormatted: false,
},
{
name: "formatEnabled is true, with args",
logger: myLogger{formatEnabled: true},
args: args{
msg: "msg is %s",
args: []any{"test"},
},
wantMessage: "msg is test",
wantFormatted: true,
},
{
name: "formatEnabled is true, with multiple args",
logger: myLogger{formatEnabled: true},
args: args{
msg: "msg is %s",
args: []any{"test", "test2"},
},
wantMessage: "msg is %s",
wantFormatted: false,
},
{
name: "formatEnabled is true, with Filed args",
logger: myLogger{formatEnabled: true},
args: args{
msg: "msg is %s",
args: []any{String("k", "v")},
},
wantMessage: "msg is %s",
wantFormatted: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &Logger{
core: tt.logger.core,
name: tt.logger.name,
addCaller: tt.logger.addCaller,
stackLevel: tt.logger.stackLevel,
callerSkip: tt.logger.callerSkip,
formatEnabled: tt.logger.formatEnabled,
contextHandlers: tt.logger.contextHandlers,
}
gotMessage, gotFormatted := l.formatMessage(tt.args.msg, tt.args.args)
assert.Equalf(t, tt.wantMessage, gotMessage, "formatMessage(%v, %v)", tt.args.msg, tt.args.args)
assert.Equalf(t, tt.wantFormatted, gotFormatted, "formatMessage(%v, %v)", tt.args.msg, tt.args.args)
})
}
}
func TestLogger_With(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
logWithFields := logger.With("key1", "value1", "key2", "value2", String("key3", "value3"))
assert.NotNil(t, logWithFields, "Expected logger with fields")
assert.Equal(t, 3, len(core.fields), "Expected fields to be added to core")
}
func TestLogger_WithOptions(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
opt := WithName("test_logger")
loggerWithOptions := logger.WithOptions(opt)
assert.Equal(t, "test_logger", loggerWithOptions.name, "Expected logger name to be set")
}
func TestLogger_WithFormat(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
loggerWithFormat := logger.WithFormat(true)
assert.True(t, loggerWithFormat.formatEnabled, "Expected format to be enabled")
loggerWithFormat = logger.WithFormat(false)
assert.False(t, loggerWithFormat.formatEnabled, "Expected format to be disabled")
}
func TestLogger_WithFormatEnable(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
loggerWithFormat := logger.WithFormatEnable()
assert.True(t, loggerWithFormat.formatEnabled, "Expected format to be enabled")
}
func TestLogger_WithFormatDisable(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
loggerWithFormat := logger.WithFormatDisable()
assert.False(t, loggerWithFormat.formatEnabled, "Expected format to be disabled")
}
func TestLogger_WithContext(t *testing.T) {
core := &mockCore{}
logger := NewLogger(
core,
WithContextHandlers(
BuildContextHandler("k1"),
BuildContextHandler("key2", "k2"),
func(ctx context.Context, record *Record) {
key := "k3"
if v := ctx.Value(key); v != nil {
record.AddFields(Any(key, v))
}
},
),
)
ctx := context.Background()
ctx = context.WithValue(ctx, "k1", "v1")
ctx = context.WithValue(ctx, "key2", 2)
logger = logger.WithContext(ctx)
assert.Equal(t, core.fields, []Field{
String("k1", "v1"),
Int("k2", 2),
}, "Expected context to be added to core")
}
func TestLogger_Enabled(t *testing.T) {
core := &mockCore{enabled: true}
logger := NewLogger(core)
assert.True(t, logger.Enabled(LevelInfo), "Expected logger to be enabled")
}
func Test_countPercent(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want int
}{
{
name: "No format here",
args: args{
s: "No format here",
},
want: 0,
},
{
name: "Hello %s, how are you?",
args: args{
s: "Hello %s, how are you?",
},
want: 1,
},
{
name: "Hello %%s, how are you?",
args: args{
s: "Hello %%s, how are you?",
},
want: 0,
},
{
name: "Hello %s%%, how are you?",
args: args{
s: "Hello %s%%, how are you?",
},
want: 1,
},
{
name: "Hello %%s%%, how are you?",
args: args{
s: "Hello %%s%%, how are you?",
},
want: 0,
},
{
name: "Hello %%%s, how are you?",
args: args{
s: "Hello %%%s, how are you?",
},
want: 1,
},
{
name: "Hello %%%%s, how are you?",
args: args{
s: "Hello %%%%s, how are you?",
},
want: 0,
},
{
name: "Empty string",
args: args{
s: "",
},
want: 0,
},
{
name: "Only percent signs",
args: args{
s: "%%%%",
},
want: 0,
},
{
name: "Single percent sign",
args: args{
s: "%",
},
want: 1,
},
{
name: "Multiple percent signs",
args: args{
s: "%%%s%%%d",
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, countPercent(tt.args.s), "countPercent(%v)", tt.args.s)
})
}
}
func TestLogger_check(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core)
t.Run("check returns nil if core is not enabled", func(t *testing.T) {
core.enabled = false
ce := logger.check(LevelInfo, "test message")
assert.Nil(t, ce, "Expected check to return nil when core is not enabled")
})
t.Run("check returns CheckedEntry if core is enabled", func(t *testing.T) {
core.enabled = true
ce := logger.check(LevelInfo, "test message")
assert.NotNil(t, ce, "Expected check to return CheckedEntry when core is enabled")
assert.Equal(t, "test message", ce.Message, "Expected message to be set in CheckedEntry")
})
t.Run("check adds caller information if addCaller is true", func(t *testing.T) {
logger.addCaller = true
ce := logger.check(LevelInfo, "test message")
assert.NotNil(t, ce.Caller, "Expected caller information to be added")
assert.True(t, ce.Caller.Defined, "Expected caller to be defined")
})
t.Run("check adds stack trace if stackLevel is enabled", func(t *testing.T) {
logger.stackLevel = LevelEnablerFunc(func(lvl Level) bool {
return lvl == LevelInfo
})
ce := logger.check(LevelInfo, "test message")
assert.NotEmpty(t, ce.Stack, "Expected stack trace to be added")
})
}
func TestLogger_log(t *testing.T) {
core := &mockCore{}
logger := NewLogger(core, WithContextHandlers(BuildContextHandler("key")))
t.Run("log without context", func(t *testing.T) {
core.reset()
core.enabled = true
logger.log(nil, LevelInfo, "test message", String("key", "value"))
assert.Len(t, core.entries, 1, "Expected one log entry")
assert.Equal(t, "test message", core.entries[0].Message, "Expected message to be 'test message'")
assert.Contains(t, core.fields, String("key", "value"), "Expected fields to contain 'key: value'")
})
t.Run("log with context", func(t *testing.T) {
core.reset()
core.enabled = true
ctx := context.WithValue(context.Background(), "key", "value")
logger.log(ctx, LevelInfo, "test message")
assert.Len(t, core.entries, 1, "Expected one log entry")
assert.Equal(t, "test message", core.entries[0].Message, "Expected message to be 'test message'")
assert.Contains(t, core.fields, String("key", "value"), "Expected fields to contain 'key: value'")
})
t.Run("log with formatted message", func(t *testing.T) {
core.reset()
core.enabled = true
logger = logger.WithFormatEnable()
logger.log(nil, LevelInfo, "Hello %s", "world")
assert.Len(t, core.entries, 1, "Expected one log entry")
assert.Equal(t, "Hello world", core.entries[0].Message, "Expected formatted message to be 'Hello world'")
})
t.Run("log with stack trace", func(t *testing.T) {
core.reset()
core.enabled = true
logger.stackLevel = LevelEnablerFunc(func(lvl Level) bool {
return lvl == LevelInfo
})
logger.log(nil, LevelInfo, "test message")
assert.Len(t, core.entries, 1, "Expected one log entry")
assert.NotEmpty(t, core.entries[0].Stack, "Expected stack trace to be added")
})
t.Run("log with caller information", func(t *testing.T) {
core.reset()
core.enabled = true
logger.addCaller = true
logger.log(nil, LevelInfo, "test message")
assert.Len(t, core.entries, 1, "Expected one log entry")
assert.True(t, core.entries[0].Caller.Defined, "Expected caller information to be added")
})
}