-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
379 lines (329 loc) · 9.45 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
package soba_test
import (
"context"
"fmt"
"testing"
"github.com/novln/soba"
)
// Test logger name format.
func TestLogger_IsNameValid(t *testing.T) {
scenarios := []struct {
name string
valid bool
}{
{
// Scenario #1
name: "hello",
valid: true,
},
{
// Scenario #2
name: "hello.world",
valid: true,
},
{
// Scenario #3
name: "Hello.World",
valid: false,
},
{
// Scenario #4
name: "Hello/World",
valid: false,
},
{
// Scenario #5
name: "hello/world",
valid: false,
},
{
// Scenario #6
name: "hello.world0",
valid: true,
},
{
// Scenario #7
name: "hello_world0",
valid: true,
},
{
// Scenario #8
name: "0hello_world0",
valid: false,
},
{
// Scenario #9
name: "_hello_world_",
valid: false,
},
{
// Scenario #10
name: "hello_world_",
valid: false,
},
{
// Scenario #11
name: "hello_world",
valid: true,
},
{
// Scenario #12
name: "xx",
valid: false,
},
{
// Scenario #13
name: "",
valid: false,
},
{
// Scenario #14
name: "foo",
valid: true,
},
{
// Scenario #15
name: "hello-world",
valid: true,
},
{
// Scenario #16
name: "hello-world0",
valid: true,
},
}
for i, scenario := range scenarios {
message := fmt.Sprintf("scenario #%d", (i + 1))
result := soba.IsLoggerNameValid(scenario.name)
if result != scenario.valid {
t.Fatalf("Unexpected result for %s: %v should be %v", message, result, scenario.valid)
}
}
}
// Test logger constructor using context.
func TestLogger_New(t *testing.T) {
conf := &soba.Config{
Loggers: map[string]soba.ConfigLogger{},
Appenders: map[string]soba.ConfigAppender{
"stdout": {
Type: "console",
},
},
Root: soba.ConfigLogger{
Level: "disabled",
Additive: false,
Appenders: []string{
"stdout",
},
},
}
ctx := context.Background()
{
ctx, err := soba.LoadWithConfig(ctx, conf)
if err != nil {
t.Fatal(err)
}
logger := soba.New(ctx, "views.greetings")
if logger.Name() != "views.greetings" {
t.Fatalf("Unexpected logger name: %s should be %s", logger.Name(), "views.greetings")
}
if logger.Level() != soba.NoLevel {
t.Fatalf("Unexpected logger name: %s should be %s", logger.Level(), soba.NoLevel)
}
}
{
defer func() {
thr := recover()
if thr == nil {
t.Fatal("A panic was expected")
}
}()
logger := soba.New(ctx, "views.greetings")
_ = logger
}
}
// Test logger filters on debug level.
func TestLogger_DebugLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.DebugLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 4 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 4)
}
expected1 := fmt.Sprint(
`{"logger":"foobar","level":"debug","message":"Debug level","idx":1}`,
"\n",
)
expected2 := fmt.Sprint(
`{"logger":"foobar","level":"info","message":"Info level","idx":2}`,
"\n",
)
expected3 := fmt.Sprint(
`{"logger":"foobar","level":"warning","message":"Warn level","idx":3}`,
"\n",
)
expected4 := fmt.Sprint(
`{"logger":"foobar","level":"error","message":"Error level","idx":4}`,
"\n",
)
if appender.Log(0) != expected1 {
t.Fatalf("Unexpected log message #1: '%s' should be '%s'", appender.Log(0), expected1)
}
if appender.Log(1) != expected2 {
t.Fatalf("Unexpected log message #2: '%s' should be '%s'", appender.Log(1), expected2)
}
if appender.Log(2) != expected3 {
t.Fatalf("Unexpected log message #3: '%s' should be '%s'", appender.Log(2), expected3)
}
if appender.Log(3) != expected4 {
t.Fatalf("Unexpected log message #4: '%s' should be '%s'", appender.Log(3), expected4)
}
}
// Test logger filters on info level.
func TestLogger_InfoLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.InfoLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 3 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 3)
}
expected1 := fmt.Sprint(
`{"logger":"foobar","level":"info","message":"Info level","idx":2}`,
"\n",
)
expected2 := fmt.Sprint(
`{"logger":"foobar","level":"warning","message":"Warn level","idx":3}`,
"\n",
)
expected3 := fmt.Sprint(
`{"logger":"foobar","level":"error","message":"Error level","idx":4}`,
"\n",
)
if appender.Log(0) != expected1 {
t.Fatalf("Unexpected log message #1: '%s' should be '%s'", appender.Log(0), expected1)
}
if appender.Log(1) != expected2 {
t.Fatalf("Unexpected log message #2: '%s' should be '%s'", appender.Log(1), expected2)
}
if appender.Log(2) != expected3 {
t.Fatalf("Unexpected log message #3: '%s' should be '%s'", appender.Log(2), expected3)
}
}
// Test logger filters on warning level.
func TestLogger_WarnLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.WarnLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 2 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 4)
}
expected1 := fmt.Sprint(
`{"logger":"foobar","level":"warning","message":"Warn level","idx":3}`,
"\n",
)
expected2 := fmt.Sprint(
`{"logger":"foobar","level":"error","message":"Error level","idx":4}`,
"\n",
)
if appender.Log(0) != expected1 {
t.Fatalf("Unexpected log message #1: '%s' should be '%s'", appender.Log(0), expected1)
}
if appender.Log(1) != expected2 {
t.Fatalf("Unexpected log message #2: '%s' should be '%s'", appender.Log(1), expected2)
}
}
// Test logger filters on error level.
func TestLogger_ErrorLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.ErrorLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 1 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 1)
}
expected1 := fmt.Sprint(
`{"logger":"foobar","level":"error","message":"Error level","idx":4}`,
"\n",
)
if appender.Log(0) != expected1 {
t.Fatalf("Unexpected log message #1: '%s' should be '%s'", appender.Log(0), expected1)
}
}
// Test logger filters on no level.
func TestLogger_NoLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.NoLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 0 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 0)
}
}
// Test logger filters on unknown level should behave as no level.
func TestLogger_UnknownLevel(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.UnknownLevel, []soba.Appender{appender})
logger.Debug("Debug level", soba.Int("idx", 1))
logger.Info("Info level", soba.Int("idx", 2))
logger.Warn("Warn level", soba.Int("idx", 3))
logger.Error("Error level", soba.Int("idx", 4))
if appender.Size() != 0 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 0)
}
}
// Test logger with custom fields.
func TestLogger_WithFields(t *testing.T) {
appender := NewTestAppender("foobar")
defer CloseAppender(t, appender)
logger := soba.NewLogger("foobar", soba.InfoLevel, []soba.Appender{appender})
logger = logger.With(soba.String("module", "xyz"), soba.Bool("shared", true))
logger.Info("Random message 1", soba.Int("id", 1))
logger.Info("Random message 2", soba.Int("id", 2))
logger.Info("Random message 3", soba.Int("id", 3))
if appender.Size() != 3 {
t.Fatalf("Unexpected number of entries for appender: %d should be %d", appender.Size(), 3)
}
expected1 := fmt.Sprint(
`{"logger":"foobar","level":"info","message":"Random message 1",`,
`"module":"xyz","shared":true,"id":1}`,
"\n",
)
expected2 := fmt.Sprint(
`{"logger":"foobar","level":"info","message":"Random message 2",`,
`"module":"xyz","shared":true,"id":2}`,
"\n",
)
expected3 := fmt.Sprint(
`{"logger":"foobar","level":"info","message":"Random message 3",`,
`"module":"xyz","shared":true,"id":3}`,
"\n",
)
if appender.Log(0) != expected1 {
t.Fatalf("Unexpected log message #1: '%s' should be '%s'", appender.Log(0), expected1)
}
if appender.Log(1) != expected2 {
t.Fatalf("Unexpected log message #2: '%s' should be '%s'", appender.Log(1), expected2)
}
if appender.Log(2) != expected3 {
t.Fatalf("Unexpected log message #3: '%s' should be '%s'", appender.Log(2), expected3)
}
}