-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger_test.go
596 lines (537 loc) · 22 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package lgr
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoggerNoDbg(t *testing.T) {
tbl := []struct {
format string
args []interface{}
rout, rerr string
}{
{"aaa", []interface{}{}, "2018/01/07 13:02:34.000 INFO aaa\n", ""},
{"DEBUG something 123 %s", []interface{}{"aaa"}, "", ""},
{"[DEBUG] something 123 %s", []interface{}{"aaa"}, "", ""},
{"INFO something 123 %s", []interface{}{"aaa"}, "2018/01/07 13:02:34.000 INFO something 123 aaa\n", ""},
{"[INFO] something 123 %s", []interface{}{"aaa"}, "2018/01/07 13:02:34.000 INFO something 123 aaa\n", ""},
{"[INFO] something 123 %s", []interface{}{"aaa\n"}, "2018/01/07 13:02:34.000 INFO something 123 aaa\n", ""},
{"blah something 123 %s", []interface{}{"aaa"}, "2018/01/07 13:02:34.000 INFO blah something 123 aaa\n", ""},
{"WARN something 123 %s", []interface{}{"aaa"}, "2018/01/07 13:02:34.000 WARN something 123 aaa\n", ""},
{"ERROR something 123 %s", []interface{}{"aaa"}, "2018/01/07 13:02:34.000 ERROR something 123 aaa\n",
"2018/01/07 13:02:34.000 ERROR something 123 aaa\n"},
}
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Msec)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
for i, tt := range tbl {
tt := tt
rout.Reset()
rerr.Reset()
t.Run(fmt.Sprintf("check-%d", i), func(t *testing.T) {
l.Logf(tt.format, tt.args...)
assert.Equal(t, tt.rout, rout.String())
assert.Equal(t, tt.rerr, rerr.String())
})
}
}
func TestLoggerWithDbg(t *testing.T) {
tbl := []struct {
format string
args []interface{}
rout, rerr string
}{
{"aaa", []interface{}{},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) aaa\n", ""},
{"DEBUG something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"[DEBUG] something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"INFO something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"[INFO] something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"blah something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 INFO (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) blah something 123 aaa\n", ""},
{"WARN something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 WARN (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n", ""},
{"ERROR something 123 %s", []interface{}{"aaa"},
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n",
"2018/01/07 13:02:34.123 ERROR (lgr/logger_test.go:83 lgr.TestLoggerWithDbg.func2) something 123 aaa\n"},
}
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Format(FullDebug), Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
for i, tt := range tbl {
tt := tt
rout.Reset()
rerr.Reset()
t.Run(fmt.Sprintf("check-%d", i), func(t *testing.T) {
l.Logf(tt.format, tt.args...)
assert.Equal(t, tt.rout, rout.String())
assert.Equal(t, tt.rerr, rerr.String())
})
}
l = New(Debug, Out(rout), Err(rerr), Format(WithMsec)) // no caller
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.000 DEBUG something 123 err\n", rout.String())
assert.Equal(t, "", rerr.String())
l = New(Debug, Out(rout), Err(rerr), Format(ShortDebug)) // caller file only
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.000 DEBUG (lgr/logger_test.go:101) something 123 err\n", rout.String())
f := `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFunc}}) {{.Message}}`
l = New(Debug, Out(rout), Err(rerr), Format(f)) // caller func only
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.000 DEBUG (lgr.TestLoggerWithDbg) something 123 err\n", rout.String())
}
func TestLoggerWithPkg(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), Format(WithPkg))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr) something 123 err\n", rout.String())
}
func TestLoggerWithCallerDepth(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l1 := New(Debug, Out(rout), Err(rerr), Format(FullDebug), CallerDepth(1))
l1.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
f := func(l L) {
l.Logf("[DEBUG] something 123 %s", "err")
}
f(l1)
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:129 lgr.TestLoggerWithCallerDepth) something 123 err\n",
rout.String())
rout.Reset()
rerr.Reset()
l2 := New(Debug, Out(rout), Err(rerr), Format(FullDebug), CallerDepth(0))
l2.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
f(l2)
assert.Equal(t, "2018/01/07 13:02:34.123 DEBUG (lgr/logger_test.go:127 lgr.TestLoggerWithCallerDepth."+
"func2) something 123 err\n", rout.String())
}
// nolint dupl
func TestLogger_formatWithOptions(t *testing.T) {
tbl := []struct {
opts []Option
elems layout
res string
}{
{
[]Option{},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "INFO "},
"2018/01/07 13:02:34 INFO blah blah",
},
{
[]Option{Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"2018/01/07 13:02:34.000 DEBUG blah blah",
},
{
[]Option{Msec, LevelBraces},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"2018/01/07 13:02:34.000 [DEBUG] blah blah",
},
{
[]Option{CallerFile, Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFile: "file1.go", CallerLine: 12},
"2018/01/07 13:02:34.000 DEBUG {file1.go:12} blah blah",
},
{
[]Option{CallerFunc, CallerPkg},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFunc: "func1", CallerPkg: "pkg"},
"2018/01/07 13:02:34 DEBUG {func1 pkg} blah blah",
},
}
for n, tt := range tbl {
tt := tt
l := New(tt.opts...)
t.Run(strconv.Itoa(n), func(t *testing.T) {
assert.Equal(t, tt.res, l.formatWithOptions(tt.elems))
})
}
}
// nolint dupl
func TestLogger_formatWithMapper(t *testing.T) {
tbl := []struct {
opts []Option
elems layout
res string
}{
{
[]Option{},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "INFO "},
"!TM=2018/01/07 13:02:34=TM! !IF=INFO =IF! !IF=blah blah*=IF!",
},
{
[]Option{Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"!TM=2018/01/07 13:02:34.000=TM! !DG=DEBUG=DG! !DG=blah blah*=DG!",
},
{
[]Option{Msec, LevelBraces},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"!TM=2018/01/07 13:02:34.000=TM! !DG=[DEBUG]=DG! !DG=blah blah*=DG!",
},
{
[]Option{CallerFile, Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFile: "file1.go", CallerLine: 12},
"!TM=2018/01/07 13:02:34.000=TM! !DG=DEBUG=DG! !CL={file1.go:12}=CL! !DG=blah blah*=DG!",
},
{
[]Option{CallerFunc, CallerPkg},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFunc: "func1", CallerPkg: "pkg"},
"!TM=2018/01/07 13:02:34=TM! !DG=DEBUG=DG! !CL={func1 pkg}=CL! !DG=blah blah*=DG!",
},
}
mp := Mapper{
MessageFunc: func(s string) string {
return s + "*"
},
ErrorFunc: func(s string) string {
return "!ER=" + s + "=ER!"
},
WarnFunc: func(s string) string {
return "!WR=" + s + "=WR!"
},
InfoFunc: func(s string) string {
return "!IF=" + s + "=IF!"
},
DebugFunc: func(s string) string {
return "!DG=" + s + "=DG!"
},
CallerFunc: func(s string) string {
return "!CL=" + s + "=CL!"
},
TimeFunc: func(s string) string {
return "!TM=" + s + "=TM!"
},
}
for n, tt := range tbl {
tt := tt
opts := []Option{}
opts = append(opts, tt.opts...)
opts = append(opts, Map(mp))
l := New(opts...)
t.Run(strconv.Itoa(n), func(t *testing.T) {
assert.Equal(t, tt.res, l.formatWithOptions(tt.elems))
})
}
}
// nolint dupl
func TestLogger_formatWithPartialMapper(t *testing.T) {
tbl := []struct {
opts []Option
elems layout
res string
}{
{
[]Option{},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "INFO "},
"!TM=2018/01/07 13:02:34=TM! INFO blah blah*",
},
{
[]Option{Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"!TM=2018/01/07 13:02:34.000=TM! DEBUG blah blah*",
},
{
[]Option{Msec, LevelBraces},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG"},
"!TM=2018/01/07 13:02:34.000=TM! [DEBUG] blah blah*",
},
{
[]Option{CallerFile, Msec},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFile: "file1.go", CallerLine: 12},
"!TM=2018/01/07 13:02:34.000=TM! DEBUG {file1.go:12} blah blah*",
},
{
[]Option{CallerFunc, CallerPkg},
layout{DT: time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local), Message: "blah blah", Level: "DEBUG",
CallerFunc: "func1", CallerPkg: "pkg"},
"!TM=2018/01/07 13:02:34=TM! DEBUG {func1 pkg} blah blah*",
},
}
mp := Mapper{
MessageFunc: func(s string) string {
return s + "*"
},
WarnFunc: func(s string) string {
return "!WR=" + s + "=WR!"
},
TimeFunc: func(s string) string {
return "!TM=" + s + "=TM!"
},
}
for n, tt := range tbl {
tt := tt
opts := []Option{}
opts = append(opts, tt.opts...)
opts = append(opts, Map(mp))
l := New(opts...)
t.Run(strconv.Itoa(n), func(t *testing.T) {
assert.Equal(t, tt.res, l.formatWithOptions(tt.elems))
})
}
}
func TestLoggerWithPanic(t *testing.T) {
fatalCalls := 0
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Format(FuncDebug), Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
l.fatal = func() { fatalCalls++ }
l.Logf("PANIC oh my, panic now! %v", errors.New("bad thing happened"))
assert.Equal(t, 1, fatalCalls)
assert.Equal(t, "2018/01/07 13:02:34.000 PANIC (lgr.TestLoggerWithPanic) oh my, panic now! bad thing happened\n", rout.String())
t.Logf(rerr.String()) //nolint:govet
assert.True(t, strings.HasPrefix(rerr.String(), "2018/01/07 13:02:34.000 PANIC"))
assert.Contains(t, rerr.String(), "github.com/go-pkgz/lgr.getDump")
assert.Contains(t, rerr.String(), "/lgr/logger.go:")
rout.Reset()
rerr.Reset()
l.Logf("[FATAL] oh my, fatal error! %v", errors.New("bad thing happened"))
assert.Equal(t, 2, fatalCalls)
assert.Equal(t, "2018/01/07 13:02:34.000 FATAL (lgr.TestLoggerWithPanic) oh my, fatal error! bad thing happened\n", rout.String())
assert.Equal(t, "2018/01/07 13:02:34.000 FATAL (lgr.TestLoggerWithPanic) oh my, fatal error! bad thing happened\n", rerr.String())
rout.Reset()
rerr.Reset()
fatalCalls = 0
l = New(Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
l.fatal = func() { fatalCalls++ }
l.Logf("[PANIC] oh my, panic now! %v", errors.New("bad thing happened"))
assert.Equal(t, 1, fatalCalls)
assert.Equal(t, "2018/01/07 13:02:34 PANIC oh my, panic now! bad thing happened\n", rout.String())
assert.True(t, strings.HasPrefix(rerr.String(), "2018/01/07 13:02:34 PANIC"))
assert.Contains(t, rerr.String(), "github.com/go-pkgz/lgr.getDump")
assert.Contains(t, rerr.String(), "/lgr/logger.go:")
}
func TestLoggerErrorWithDump(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Format(FuncDebug), Out(rout), Err(rerr), StackTraceOnError)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
l.Logf("ERROR oh my, error now! %v", errors.New("bad thing happened"))
lines := strings.Split(rout.String(), "\n")
assert.Equal(t, "2018/01/07 13:02:34.000 ERROR (lgr.TestLoggerErrorWithDump) oh my, error now! bad thing happened", lines[0])
assert.Equal(t, ">>> stack trace:", lines[1])
assert.Contains(t, lines[2], "github.com/go-pkgz/lgr.TestLoggerErrorWithDump(")
assert.Contains(t, lines[3], "lgr/logger_test.go:363")
}
func TestLoggerWithErrorSameOutputs(t *testing.T) {
fatalCalls := 0
rout := bytes.NewBuffer([]byte{})
l := New(Debug, Format(FuncDebug), Out(rout), Err(rout))
l.fatal = func() { fatalCalls++ }
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
l.Logf("ERROR oh my, error now! %v", errors.New("bad thing happened"))
assert.Equal(t, "2018/01/07 13:02:34.000 ERROR (lgr.TestLoggerWithErrorSameOutputs) oh my, error now! bad thing happened\n", rout.String())
rout.Reset()
l.Logf("FATAL oh my, error now! %v", errors.New("bad thing happened"))
assert.Equal(t, "2018/01/07 13:02:34.000 FATAL (lgr.TestLoggerWithErrorSameOutputs) oh my, error now! bad thing happened\n", rout.String())
assert.Equal(t, 1, fatalCalls)
}
func TestLoggerConcurrent(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
var wg sync.WaitGroup
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func(i int) {
l.Logf("[DEBUG] test test 123 debug message #%d, %v", i, errors.New("some error"))
wg.Done()
}(i)
}
wg.Wait()
assert.Equal(t, 1001, len(strings.Split(rout.String(), "\n")))
assert.Equal(t, "", rerr.String())
}
func TestLoggerWithLevelBraces(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), Format(`{{.DT.Format "2006/01/02 15:04:05"}} [{{.Level}}] {{.Message}}`))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("[INFO] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 [INFO] something 123 err\n", rout.String())
l = New(Debug, Out(rout), Err(rerr), LevelBraces)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[ERROR] some warning 123")
assert.Equal(t, "2018/01/07 13:02:34 [ERROR] some warning 123\n", rout.String())
assert.Equal(t, "2018/01/07 13:02:34 [ERROR] some warning 123\n", rerr.String())
rout.Reset()
rerr.Reset()
l.Logf("WARN some warning 123")
assert.Equal(t, "2018/01/07 13:02:34 [WARN] some warning 123\n", rout.String())
}
func TestLoggerWithTrace(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Trace, Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("[INFO] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 INFO something 123 err\n", rout.String())
rout.Reset()
rerr.Reset()
l.Logf("[DEBUG] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 DEBUG something 123 err\n", rout.String())
rout.Reset()
rerr.Reset()
l.Logf("[TRACE] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 TRACE something 123 err\n", rout.String())
l = New(Debug, Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[TRACE] something 123 %s", "err")
assert.Equal(t, "", rout.String())
l = New(Trace, Out(rout), Err(rerr), CallerPkg)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
rout.Reset()
rerr.Reset()
l.Logf("[TRACE] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 TRACE {lgr} something 123 err\n", rout.String())
}
func TestLoggerWithInvalidTemplate(t *testing.T) {
// invalid template format
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Format(`{{.DT.Format "2006/01/02 15:04:05"}} {{{.BadThing}} {{.Message}}`))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("[INFO] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 INFO something 123 err\n", rout.String(), "default format")
// invalid var
rout, rerr = bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l1 := New(Out(rout), Err(rerr), Format(`{{.DT.Format "2006/01/02 15:04:05"}} {{.BadThing}} {{.Message}}`))
l1.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l1.Logf("[INFO] something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 INFO something 123 err\n", rout.String(), "default format")
}
func TestLoggerOverwriteFormat(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), Msec, Format(Short), CallerFile) // mix Format with individual flags
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("INFO something 123 %s", "err")
assert.Equal(t, "2018/01/07 13:02:34 INFO something 123 err\n", rout.String(), "short format enforced")
}
func TestLoggerNoSpaceLevel(t *testing.T) {
tbl := []struct {
format string
args []interface{}
rout, rerr string
}{
{"INFOsomething 123 %s", []interface{}{"aaa1"}, "2018/01/07 13:02:34.000 INFO something 123 aaa1\n", ""},
{"[INFO]something 123 %s", []interface{}{"aaa1"}, "2018/01/07 13:02:34.000 INFO something 123 aaa1\n", ""},
{"[INFO]something 123 %s", []interface{}{"aaa1\n"}, "2018/01/07 13:02:34.000 INFO something 123 aaa1\n", ""},
{"WARNsomething 123 %s", []interface{}{"aaa1"}, "2018/01/07 13:02:34.000 WARN something 123 aaa1\n", ""},
{"ERRORsomething 123 %s", []interface{}{"aaa1"}, "2018/01/07 13:02:34.000 ERROR something 123 aaa1\n",
"2018/01/07 13:02:34.000 ERROR something 123 aaa1\n"},
}
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Msec)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
for i, tt := range tbl {
tt := tt
rout.Reset()
rerr.Reset()
t.Run(fmt.Sprintf("check-%d", i), func(t *testing.T) {
l.Logf(tt.format, tt.args...)
assert.Equal(t, tt.rout, rout.String())
assert.Equal(t, tt.rerr, rerr.String())
})
}
}
func TestLoggerHidden(t *testing.T) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Format(Short), Secret("password", "secret", "", " "))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 123000000, time.Local) }
l.Logf("INFO something password 123 secret xyz")
assert.Equal(t, "2018/01/07 13:02:34 INFO something ****** 123 ****** xyz\n", rout.String(), "secrets secrets")
}
func TestIsStreamsSame(t *testing.T) {
{ // with stdout and stderr
sout, serr := os.Stdout, os.Stderr
assert.True(t, isStreamsSame(sout, serr))
}
{ // with same file
dir := t.TempDir()
f, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f.Name())
assert.True(t, isStreamsSame(f, f))
}
{ // with different files
dir := t.TempDir()
f1, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f1.Name())
f2, err := os.CreateTemp(dir, "test")
require.NoError(t, err)
defer os.Remove(f2.Name())
assert.False(t, isStreamsSame(f1, f2))
}
{ // with same buffer
buf := bytes.NewBuffer([]byte{})
assert.True(t, isStreamsSame(buf, buf))
}
{ // with different buffers
buf1, buf2 := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
sout, serr := buf1, buf2
assert.False(t, isStreamsSame(sout, serr))
}
}
func BenchmarkNoDbgNoFormat(b *testing.B) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
e := errors.New("some error")
for n := 0; n < b.N; n++ {
l.Logf("[INFO] test test 123 debug message #%d, %v", n, e)
}
}
func BenchmarkNoDbgFormat(b *testing.B) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Out(rout), Err(rerr), Format(Short))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
e := errors.New("some error")
for n := 0; n < b.N; n++ {
l.Logf("[INFO] test test 123 debug message #%d, %v", n, e)
}
}
func BenchmarkWithDbgNoFormat(b *testing.B) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Out(rout), Err(rerr), CallerFile, CallerFunc, CallerPkg)
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
e := errors.New("some error")
for n := 0; n < b.N; n++ {
l.Logf("INFO test test 123 debug message #%d, %v", n, e)
}
}
func BenchmarkWithDbgAndFormat(b *testing.B) {
rout, rerr := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
l := New(Debug, Format(FullDebug), Out(rout), Err(rerr))
l.now = func() time.Time { return time.Date(2018, 1, 7, 13, 2, 34, 0, time.Local) }
e := errors.New("some error")
for n := 0; n < b.N; n++ {
l.Logf("INFO test test 123 debug message #%d, %v", n, e)
}
}