-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
1079 lines (1025 loc) · 29.8 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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017-2023 Fortio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log // import "fortio.org/fortio/log"
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"testing"
"time"
"fortio.org/log/goroutine"
)
const thisFilename = "logger_test.go"
// leave this test first/where it is as it relies on line number not changing.
func TestLoggerFilenameLine(t *testing.T) {
SetLogLevel(Debug) // make sure it's already debug when we capture
on := true
Config.LogFileAndLine = on
Config.LogPrefix = "-prefix-"
Config.JSON = false
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetOutput(w)
SetFlags(0)
SetLogLevel(Debug)
if LogDebug() {
Debugf("test") // line 51
}
SetLogLevel(-1) // line 53
SetLogLevel(Warning) // line 54
Infof("should not show (info level)")
Printf("Should show despite being Info - unconditional Printf without line/file")
w.Flush()
actual := b.String()
expected := "[D] logger_test.go:51-prefix-test\n" +
"[E] logger_test.go:53-prefix-SetLogLevel called with level -1 lower than Debug!\n" +
"[I] logger_test.go:54-prefix-Log level is now 3 Warning (was 0 Debug)\n" +
"Should show despite being Info - unconditional Printf without line/file\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
// leave this test second/where it is as it relies on line number not changing.
func TestLoggerFilenameLineJSON(t *testing.T) {
SetLogLevel(Debug) // make sure it's already debug when we capture
on := true
Config.LogFileAndLine = on
Config.LogPrefix = "-not used-"
Config.JSON = true
Config.NoTimestamp = true
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetOutput(w)
SetLogLevel(Debug)
if LogDebug() {
Debugf("a test") // line 81
}
w.Flush()
actual := b.String()
grID := goroutine.ID()
if grID <= 0 {
t.Errorf("unexpected goroutine id %d", grID)
}
expected := `{"level":"dbug","r":` + strconv.FormatInt(grID, 10) +
`,"file":"` + thisFilename + `","line":81,"msg":"a test"}` + "\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func Test_LogS_JSON_no_json_with_filename(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Warning"))
Config.LogFileAndLine = true
Config.JSON = false
Config.NoTimestamp = false
Config.LogPrefix = "-bar-"
log.SetFlags(0)
SetOutput(w)
// Start of the actual test
S(Verbose, "This won't show")
S(Warning, "This will show", Str("key1", "value 1"), Attr("key2", 42)) // line 109
Printf("This will show too") // no filename/line and shows despite level
_ = w.Flush()
actual := b.String()
expected := "[W] logger_test.go:109-bar-This will show, key1=\"value 1\", key2=42\n" +
"This will show too\n"
if actual != expected {
t.Errorf("got %q expected %q", actual, expected)
}
}
func TestColorMode(t *testing.T) {
if ConsoleLogging() {
t.Errorf("expected not to be console logging")
}
if Color {
t.Errorf("expected to not be in color mode initially")
}
// Setup
Config = DefaultConfig()
Config.ForceColor = true
Config.NoTimestamp = true
Config.LogPrefix = "" // test it'll be at least one space
SetLogLevelQuiet(Info)
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetOutput(w) // will call SetColorMode()
if !Color {
t.Errorf("expected to be in color mode after ForceColor=true and SetColorMode()")
}
S(Warning, "With file and line", String("attr", "value with space")) // line 139
Infof("info with file and line = %v", Config.LogFileAndLine) // line 140
Config.LogFileAndLine = false
Config.GoroutineID = false
S(Warning, "Without file and line", Str("attr", "value with space"))
Infof("info with file and line = %v", Config.LogFileAndLine)
_ = w.Flush()
actual := b.String()
grID := fmt.Sprintf("r%d ", goroutine.ID())
expected := "\x1b[37m" + grID + "\x1b[90m[\x1b[33mWRN\x1b[90m] logger_test.go:139 " +
"\x1b[33mWith file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" +
"\x1b[37m" + grID + "\x1b[90m[\x1b[32mINF\x1b[90m] logger_test.go:140 \x1b[32minfo with file and line = true\x1b[0m\n" +
"\x1b[90m[\x1b[33mWRN\x1b[90m] \x1b[33mWithout file and line\x1b[0m, \x1b[34mattr\x1b[0m=\x1b[33m\"value with space\"\x1b[0m\n" +
"\x1b[90m[\x1b[32mINF\x1b[90m] \x1b[32minfo with file and line = false\x1b[0m\n"
if actual != expected {
t.Errorf("got:\n%s\nexpected:\n%s", actual, expected)
}
// See color timestamp
Config.NoTimestamp = false
cs := colorTimestamp()
if cs == "" {
t.Errorf("got empty color timestamp")
}
if Colors.Green == "" {
t.Errorf("expected to have green color not empty when in color mode")
}
prevGreen := Colors.Green
// turn off color mode
Config.ForceColor = false
SetColorMode()
if Color {
t.Errorf("expected to not be in color mode after SetColorMode() and forcecolor false")
}
if Colors.Green != "" {
t.Errorf("expected to have green color empty when not color mode, got %q", Colors.Green)
}
if LevelToColor[Info] != "" {
t.Errorf("expected LevelToColor to be empty when not color mode, got %q", LevelToColor[Info])
}
// Show one can mutate/change/tweak colors
customColor := "foo"
ANSIColors.Green = customColor
Config.ForceColor = true
SetColorMode()
if Colors.Green != customColor {
t.Errorf("expected to have color customized, got %q", Colors.Green)
}
if LevelToColor[Info] != customColor {
t.Errorf("expected LevelToColor to the custom foo, got %q", LevelToColor[Info])
}
// put it back to real green for other tests
ANSIColors.Green = prevGreen
// Reset for other/further tests
Config.ForceColor = false
SetColorMode()
}
func TestSetLevel(t *testing.T) {
_ = SetLogLevel(Info)
err := SetLogLevelStr("debug")
if err != nil {
t.Errorf("unexpected error for valid level %v", err)
}
prev := SetLogLevel(Info)
if prev != Debug {
t.Errorf("unexpected level after setting debug %v", prev)
}
err = SetLogLevelStr("bogus")
if err == nil {
t.Errorf("Didn't get an error setting bogus level")
}
}
func TestLogger1(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(Info) // reset from other tests
Config.LogFileAndLine = false
Config.LogPrefix = ""
Config.JSON = false
SetOutput(w)
log.SetFlags(0)
// Start of the actual test
SetLogLevel(LevelByName("Verbose"))
expected := "[I] Log level is now 1 Verbose (was 2 Info)\n"
i := 0
if LogVerbose() {
LogVf("test Va %d", i) // Should show
}
i++
expected += "[V] test Va 0\n"
Warnf("test Wa %d", i) // Should show
i++
expected += "[W] test Wa 1\n"
Logger().Printf("test Logger().Printf %d", i)
i++
expected += "[I] test Logger().Printf 2\n"
SetLogLevelQuiet(Debug) // no additional logging about level change
prevLevel := SetLogLevel(LevelByName("error")) // works with lowercase too
expected += "[I] Log level is now 4 Error (was 0 Debug)\n"
LogVf("test Vb %d", i) // Should not show
Infof("test info when level is error %d", i) // Should not show
i++
Warnf("test Wb %d", i) // Should not show
i++
Errf("test E %d", i) // Should show
i++
expected += "[E] test E 5\n"
// test the rest of the api
Logf(LevelByName("Critical"), "test %d level str %s, cur %s", i, prevLevel.String(), GetLogLevel().String())
expected += "[C] test 6 level str Debug, cur Error\n"
i++
SetLogLevel(Debug) // should be fine and invisible change
SetLogLevel(Debug - 1)
expected += "[E] SetLogLevel called with level -1 lower than Debug!\n"
SetLogLevel(Fatal) // Hiding critical level is not allowed
expected += "[E] SetLogLevel called with level 6 higher than Critical!\n"
SetLogLevel(Critical) // should be fine
expected += "[I] Log level is now 5 Critical (was 0 Debug)\n"
Critf("testing crit %d", i) // should show
expected += "[C] testing crit 7\n"
Printf("Printf should always show n=%d", 8)
expected += "Printf should always show n=8\n"
r := FErrf("FErrf should always show but not exit, n=%d", 9)
expected += "[F] FErrf should always show but not exit, n=9\n"
if r != 1 {
t.Errorf("FErrf returned %d instead of 1", r)
}
_ = w.Flush()
actual := b.String()
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func TestLoggerJSON(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = true
Config.LogPrefix = "not used"
Config.JSON = true
Config.NoTimestamp = false
SetOutput(w)
// Start of the actual test
now := time.Now()
if LogVerbose() {
LogVf("Test Verbose %d", 0) // Should show
}
_ = w.Flush()
actual := b.String()
e := JSONEntry{}
err := json.Unmarshal([]byte(actual), &e)
t.Logf("got: %s -> %#v", actual, e)
if err != nil {
t.Errorf("unexpected JSON deserialization error %v for %q", err, actual)
}
if e.Level != "trace" {
t.Errorf("unexpected level %s", e.Level)
}
if e.Msg != "Test Verbose 0" {
t.Errorf("unexpected body %s", e.Msg)
}
if e.File != thisFilename {
t.Errorf("unexpected file %q", e.File)
}
if e.Line < 270 || e.Line > 310 {
t.Errorf("unexpected line %d", e.Line)
}
ts := e.Time()
now = microsecondResolution(now) // truncates so can't be after ts
if now.After(ts) {
t.Errorf("unexpected time %v is after %v", now, ts)
}
if ts.Sub(now) > 100*time.Millisecond {
t.Errorf("unexpected time %v is > 1sec after %v", ts, now)
}
}
func Test_LogS_JSON(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = true
Config.JSON = true
Config.NoTimestamp = false
SetOutput(w)
// Start of the actual test
now := time.Now()
value2 := 42
value3 := 3.14
S(Verbose, "Test Verbose", Str("key1", "value 1"), Int("key2", value2), Float64("key3", value3))
_ = w.Flush()
actual := b.String()
e := JSONEntry{}
err := json.Unmarshal([]byte(actual), &e)
t.Logf("got: %s -> %#v", actual, e)
if err != nil {
t.Errorf("unexpected JSON deserialization error %v for %q", err, actual)
}
if e.Level != "trace" {
t.Errorf("unexpected level %s", e.Level)
}
if e.Msg != "Test Verbose" {
t.Errorf("unexpected body %s", e.Msg)
}
if e.File != thisFilename {
t.Errorf("unexpected file %q", e.File)
}
if e.Line < 270 || e.Line > 340 {
t.Errorf("unexpected line %d", e.Line)
}
ts := e.Time()
now = microsecondResolution(now) // truncates so can't be after ts
if now.After(ts) {
t.Errorf("unexpected time %v is after %v", now, ts)
}
if ts.Sub(now) > 100*time.Millisecond {
t.Errorf("unexpected time %v is > 1sec after %v", ts, now)
}
// check extra attributes
var tmp map[string]interface{}
err = json.Unmarshal([]byte(actual), &tmp)
if err != nil {
t.Errorf("unexpected JSON deserialization 2 error %v for %q", err, actual)
}
if tmp["key1"] != "value 1" {
t.Errorf("unexpected key1 %v", tmp["key1"])
}
if tmp["key2"] != float64(42) {
t.Errorf("unexpected key2 %v", tmp["key2"])
}
if tmp["key3"] != 3.14 { // comparing floats with == is dicey but... this passes...
t.Errorf("unexpected key3 %v", tmp["key3"])
}
if tmp["file"] != thisFilename {
t.Errorf("unexpected file %v", tmp["file"])
}
}
func Test_LogS_JSON_no_file(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Warning"))
Config.LogFileAndLine = false
Config.JSON = true
Config.NoTimestamp = false
SetOutput(w)
// Start of the actual test
S(Verbose, "This won't show")
S(Warning, "This will show", Attr("key1", "value 1"))
_ = w.Flush()
actual := b.String()
var tmp map[string]interface{}
err := json.Unmarshal([]byte(actual), &tmp)
if err != nil {
t.Errorf("unexpected JSON deserialization error %v for %q", err, actual)
}
if tmp["key1"] != "value 1" {
t.Errorf("unexpected key1 %v", tmp["key1"])
}
if tmp["file"] != nil {
t.Errorf("unexpected file %v", tmp["file"])
}
}
func Test_LogS_JSON_no_json_no_file(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Warning"))
Config.LogFileAndLine = false
Config.JSON = false
Config.NoTimestamp = false
Config.LogPrefix = "-foo-"
log.SetFlags(0)
SetOutput(w)
// Start of the actual test
S(Verbose, "This won't show")
S(Warning, "This will show", Str("key1", "value 1"), Attr("key2", 42))
S(NoLevel, "This NoLevel will show despite logically info level")
_ = w.Flush()
actual := b.String()
expected := "[W]-foo-This will show, key1=\"value 1\", key2=42\n" +
"This NoLevel will show despite logically info level\n"
if actual != expected {
t.Errorf("---got:---\n%s\n---expected:---\n%s\n", actual, expected)
}
}
func TestLoggerJSONNoTimestampNoFilename(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = false
Config.LogPrefix = "no used"
Config.JSON = true
Config.NoTimestamp = true
SetOutput(w)
// Start of the actual test
Critf("Test Critf")
_ = w.Flush()
actual := b.String()
e := JSONEntry{}
err := json.Unmarshal([]byte(actual), &e)
t.Logf("got: %s -> %#v", actual, e)
if err != nil {
t.Errorf("unexpected JSON deserialization error %v for %q", err, actual)
}
if e.Level != "crit" {
t.Errorf("unexpected level %s", e.Level)
}
if e.Msg != "Test Critf" {
t.Errorf("unexpected body %s", e.Msg)
}
if e.File != "" {
t.Errorf("unexpected file %q", e.File)
}
if e.Line != 0 {
t.Errorf("unexpected line %d", e.Line)
}
if e.TS != 0 {
t.Errorf("unexpected time should be absent, got %v %v", e.TS, e.Time())
}
}
func TestLoggerSimpleJSON(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = false
Config.LogPrefix = "no used"
Config.JSON = true
Config.NoTimestamp = false
SetOutput(w)
// Start of the actual test
w.WriteString("[")
Critf("Test Critf2")
w.WriteString(",")
S(Critical, "Test Critf3")
w.WriteString("]")
_ = w.Flush()
actual := b.String()
e := []JSONEntry{}
err := json.Unmarshal([]byte(actual), &e)
t.Logf("got: %s -> %#v", actual, e)
if err != nil {
t.Errorf("unexpected JSON deserialization error %v for %q", err, actual)
}
if len(e) != 2 {
t.Errorf("unexpected number of entries %d", len(e))
}
for i := 0; i < 2; i++ {
e := e[i]
if e.Level != "crit" {
t.Errorf("unexpected level %s", e.Level)
}
exp := fmt.Sprintf("Test Critf%d", i+2)
if e.Msg != exp {
t.Errorf("unexpected body %s", e.Msg)
}
if e.File != "" {
t.Errorf("unexpected file %q", e.File)
}
if e.Line != 0 {
t.Errorf("unexpected line %d", e.Line)
}
if e.TS == 0 {
t.Errorf("unexpected 0 time should have been present")
}
}
}
// Test that TimeToTs and Time() are inverse of one another.
func TestTimeToTs(t *testing.T) {
var prev float64
// tight loop to get different times, at highest resolution
for i := 0; i < 100000; i++ {
now := time.Now()
// now = now.Add(69 * time.Nanosecond)
usecTSstr := timeToTStr(now)
usecTS, _ := strconv.ParseFloat(usecTSstr, 64)
if i != 0 && usecTS < prev {
t.Fatalf("clock went back in time at iter %d %v vs %v", i, usecTS, prev)
}
prev = usecTS
e := JSONEntry{TS: usecTS}
inv := e.Time()
// Round to microsecond because that's the resolution of the timestamp
// (note that on a mac for instance, there is no nanosecond resolution anyway)
if !microsecondResolution(now).Equal(inv) {
t.Fatalf("[at %d] unexpected time %v -> %v != %v (%v %v)", i, now, microsecondResolution(now), inv, usecTS, usecTSstr)
}
}
}
func microsecondResolution(t time.Time) time.Time {
// Truncate and not Round because that's what UnixMicro does (indirectly).
return t.Truncate(1 * time.Microsecond)
}
// concurrency test, make sure json aren't mixed up.
func TestLoggerJSONConcurrency(t *testing.T) {
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetLogLevel(LevelByName("Verbose"))
Config.LogFileAndLine = true
Config.NoTimestamp = true
Config.JSON = true
SetOutput(w)
// Start of the actual test
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func(i int) {
for j := 0; j < 100; j++ {
Infof("Test from %d: %d", i, j)
}
wg.Done()
}(i)
}
wg.Wait()
_ = w.Flush()
actual := b.String()
t.Logf("got: %s", actual)
// Check it all deserializes to JSON correctly and we get the expected number of lines
count := 0
for _, line := range strings.Split(actual, "\n") {
if count == 1000 && line == "" {
// last line is empty
continue
}
count++
e := JSONEntry{}
err := json.Unmarshal([]byte(line), &e)
if err != nil {
t.Errorf("unexpected JSON deserialization error on line %d %v for %q", count, err, line)
}
}
if count != 1000 {
t.Errorf("unexpected number of lines %d", count)
}
}
func TestLogFatal(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic from log.Fatalf, didn't get one")
}
}()
Fatalf("test of log fatal")
}
func TestLoggerFatalCliMode(t *testing.T) {
SetDefaultsForClientTools()
if os.Getenv("DO_LOG_FATALF") == "1" {
Fatalf("test")
Errf("should have exited / this shouldn't have been reached")
return // will cause exit status 0 if reached and thus fail the test
}
// unfortunately, even if passing -test.coverprofile it doesn't get counted
cmd := exec.Command(os.Args[0], "-test.run=TestLoggerFatalCliMode")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Env = append(os.Environ(), "DO_LOG_FATALF=1")
err := cmd.Run()
var e *exec.ExitError
if ok := errors.As(err, &e); ok && e.ExitCode() == 1 {
Printf("Got expected exit status 1")
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}
func TestLoggerFatalExitOverride(t *testing.T) {
SetDefaultsForClientTools()
exitCalled := false
Config.FatalExit = func(_ int) {
exitCalled = true
}
Fatalf("testing log.Fatalf exit case")
if !exitCalled {
t.Error("expected exit function override not called")
}
}
func TestMultipleFlags(t *testing.T) {
SetLogLevelQuiet(Verbose)
// use x... so it's sorted after the standard loglevel for package level
// print default tests were all 3 flags are present.
LoggerStaticFlagSetup("xllvl1", "xllvl2")
f := flag.Lookup("loglevel")
if f != nil {
t.Error("expected default loglevel to not be registered")
}
f = flag.Lookup("xllvl1")
if f.Value.String() != "Verbose" {
t.Errorf("expected flag default value to be Verbose, got %s", f.Value.String())
}
if err := f.Value.Set(" iNFo\n"); err != nil {
t.Errorf("expected flag to be settable, got %v", err)
}
f2 := flag.Lookup("xllvl2")
if f2.Value.String() != "Info" {
t.Errorf("expected linked flag value to be Info, got %s", f2.Value.String())
}
if GetLogLevel() != Info {
t.Errorf("expected log level to be Info, got %s", GetLogLevel().String())
}
if err := f2.Value.Set("debug"); err != nil {
t.Errorf("expected flag2 to be settable, got %v", err)
}
if GetLogLevel() != Debug {
t.Errorf("expected log level to be Debug, got %s", GetLogLevel().String())
}
}
func TestStaticFlagDefault(t *testing.T) {
SetLogLevelQuiet(Warning)
LoggerStaticFlagSetup()
var b bytes.Buffer
flag.CommandLine.SetOutput(&b)
flag.CommandLine.PrintDefaults()
s := b.String()
expected := " -loglevel level\n" +
" \tlog level, one of [Debug Verbose Info Warning Error Critical Fatal] " +
"(default Warning)\n"
if !strings.HasPrefix(s, expected) {
t.Errorf("expected flag output to start with %q, got %q", expected, s)
}
f := flag.Lookup("loglevel")
if f == nil {
t.Fatal("expected flag to be registered")
}
if f.Value.String() != "Warning" {
t.Errorf("expected flag default value to be Warning, got %s", f.Value.String())
}
if err := f.Value.Set("badlevel"); err == nil {
t.Error("expected error passing a bad level value, didn't get one")
}
if err := f.Value.Set(" iNFo\n"); err != nil {
t.Errorf("expected flag to be settable, got %v", err)
}
if GetLogLevel() != Info {
t.Errorf("expected log level to be Info, got %s", GetLogLevel().String())
}
}
func TestTimeToTS(t *testing.T) {
// test a few times and expected string
for _, tst := range []struct {
sec int64
nano int64
result string
}{
{1688763601, 42000, "1688763601.000042"}, // 42 usec after the seconds part, checking for leading zeroes
{1688763601, 199999999, "1688763601.199999"}, // nanosec are truncated away not rounded (see note in TimeToTS)
{1688763601, 200000999, "1688763601.200000"}, // boundary
{1689983019, 142600000, "1689983019.142600"}, // trailing zeroes
} {
tm := time.Unix(tst.sec, tst.nano)
ts := timeToTStr(tm)
if ts != tst.result {
t.Errorf("unexpected ts for %d, %d -> %q instead of %q (%v)", tst.sec, tst.nano, ts, tst.result, tm)
}
}
}
func TestJSONLevelReverse(t *testing.T) {
str := LevelToJSON[Warning]
if str != `"warn"` {
t.Errorf("unexpected JSON level string %q (should have quotes)", str)
}
lvl := JSONStringLevelToLevel["warn"]
if lvl != Warning {
t.Errorf("unexpected level %d", lvl)
}
lvl = JSONStringLevelToLevel["info"] // Should be info and not NoLevel (7)
if lvl != Info {
t.Errorf("unexpected level %d", lvl)
}
lvl = JSONStringLevelToLevel["fatal"] // Should be info and not NoLevel (7)
if lvl != Fatal {
t.Errorf("unexpected level %d", lvl)
}
}
func TestNoLevel(t *testing.T) {
Config.ForceColor = true
SetColorMode()
color := ColorLevelToStr(NoLevel)
if color != ANSIColors.DarkGray {
t.Errorf("unexpected color %q", color)
}
Config.ForceColor = false
Config.JSON = true
Config.ConsoleColor = false
Config.NoTimestamp = true
Config.GoroutineID = false
var buf bytes.Buffer
SetOutput(&buf)
Printf("test")
actual := buf.String()
expected := `{"level":"info","msg":"test"}` + "\n"
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
type customError struct {
Msg string
Code int
}
type customErrorAlias customError
func (e customError) Error() string {
return fmt.Sprintf("custom error %s (code %d)", e.Msg, e.Code)
}
func (e customError) MarshalJSON() ([]byte, error) {
return json.Marshal(customErrorAlias(e))
}
func TestPointers(t *testing.T) {
var iPtr *int
kv := Any("err", iPtr)
kvStr := kv.StringValue()
expected := `null`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
i := 42
iPtr = &i
kv = Any("int", iPtr)
kvStr = kv.StringValue()
expected = `42`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
var sPtr *string
kv = Any("msg", sPtr)
kvStr = kv.StringValue()
expected = `null`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
s := "test\nline2"
sPtr = &s
kv = Any("msg", sPtr)
kvStr = kv.StringValue()
expected = `"test\nline2"`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
}
func TestMoreTypes(t *testing.T) {
var b byte = 42
kv := Any("byte", b)
kvStr := kv.StringValue()
expected := `42`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
runes := []rune(`A"Φ`) // test plain ascii, a double quote, and multibyte
r := runes[0]
kv = Rune("rune", r)
kvStr = kv.StringValue()
expected = `"A"`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
r = runes[1]
kv = Rune("rune", r)
kvStr = kv.StringValue()
expected = `"\""`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
r = runes[2]
kv = Rune("rune", r)
kvStr = kv.StringValue()
expected = `"Φ"`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
}
func TestStruct(t *testing.T) {
type testStruct struct {
Msg1 string
Msg2 *string
}
ptrStr := "test2"
ts := testStruct{Msg1: "test\nline2", Msg2: &ptrStr}
kv := Any("ts", ts)
kvStr := kv.StringValue()
expected := `{"Msg1":"test\nline2","Msg2":"test2"}`
if !fullJSON {
expected = `"{Msg1:test\nline2 Msg2:`
expected += fmt.Sprintf("%p}\"", &ptrStr)
}
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
}
func TestSerializationOfError(t *testing.T) {
var err error
kv := Any("err", err)
kvStr := kv.StringValue()
expected := `null`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
err = errors.New("test error")
Errf("Error on purpose: %v", err)
S(Error, "Error on purpose", Any("err", err))
kv = Any("err", err)
kvStr = kv.StringValue()
expected = `"test error"`
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
err = customError{Msg: "custom error", Code: 42}
kv = Any("err", err)
kvStr = kv.StringValue()
expected = `{"Msg":"custom error","Code":42}`
if !fullJSON {
expected = `"custom error custom error (code 42)"`
}
if kvStr != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", kvStr, expected)
}
}
func TestEnvHelp(t *testing.T) {
SetDefaultsForClientTools()
Config.NoTimestamp = false
// Setup
var b bytes.Buffer
w := bufio.NewWriter(&b)
EnvHelp(w)
w.Flush()
actual := b.String()
expected := `# Logger environment variables:
LOGGER_LOG_PREFIX=' '
LOGGER_LOG_FILE_AND_LINE=false
LOGGER_FATAL_PANICS=false
LOGGER_JSON=false
LOGGER_NO_TIMESTAMP=false
LOGGER_CONSOLE_COLOR=true
LOGGER_FORCE_COLOR=false
LOGGER_GOROUTINE_ID=false
LOGGER_COMBINE_REQUEST_AND_RESPONSE=false
LOGGER_LEVEL='Info'
LOGGER_IGNORE_CLI_MODE=false
`
if actual != expected {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func TestConfigFromEnvError(t *testing.T) {
t.Setenv("LOGGER_LEVEL", "foo")
var buf bytes.Buffer
SetOutput(&buf)
configFromEnv()
actual := buf.String()
expected := "Invalid log level from environment"
if !strings.Contains(actual, expected) {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
}
func TestConfigFromEnvOk(t *testing.T) {
t.Setenv("LOGGER_LEVEL", "verbose")
t.Setenv("LOGGER_IGNORE_CLI_MODE", "true")
t.Setenv("LOGGER_LOG_FILE_AND_LINE", "true") // earlier test disable this
var buf bytes.Buffer
SetOutput(&buf)
configFromEnv()
actual := buf.String()
expected := "Log level set from environment LOGGER_LEVEL to Verbose"
if !strings.Contains(actual, expected) {
t.Errorf("unexpected:\n%s\nvs:\n%s\n", actual, expected)
}
if !Config.IgnoreCliMode {
t.Errorf("expected IgnoreCLIMode to be true")
}
if Config.LogFileAndLine != true {
t.Errorf("expected initial LogFileAndLine to be true")
}
SetDefaultsForClientTools() // should be no-op, ie not turn off LogFileAndLine etc
if Config.LogFileAndLine != true {
t.Errorf("expected LogFileAndLine to still be true after SetDefaultsForClientTools with LOGGER_IGNORE_CLI_MODE=true")
}
}
func TestInvalidFile(t *testing.T) {
if isValid(nil) {
t.Errorf("expected nil to be invalid")
}
prev := jWriter.w
invalidFile := os.NewFile(^uintptr(0), "invalid-file")
jWriter.w = invalidFile
b := ConsoleLogging()
jWriter.w = prev
if b {
t.Errorf("expected not to be console logging")
}
}
func TestConcurrentLevelSet(t *testing.T) {
// This test is to make sure that setting the log level concurrently
// doesn't cause a -race failure. Shows up in dflag/ for instance with configmap changes.
var wg sync.WaitGroup
wg.Add(int(Fatal - Verbose))
for i := Verbose; i < Fatal; i++ {
go func(lvl Level) {
SetLogLevel(lvl)
wg.Done()
}(i)
}
wg.Wait()
SetLogLevel(Info)
t.Logf("log level is now %s", GetLogLevel().String())
}
// --- Benchmarks
// This `discard` is like io.Discard, except that io.Discard is checked explicitly
// (e.g. https://cs.opensource.google/go/go/+/refs/tags/go1.22.5:src/log/log.go;l=84)
// in logger optimizations and we want to measure the actual production
// of messages.
type discard struct{}
func (discard) Write(p []byte) (int, error) {
return len(p), nil
}
func (discard) WriteString(s string) (int, error) {
return len(s), nil
}
var Discard = discard{}
func BenchmarkLogDirect1(b *testing.B) {
setLevel(Error)
for n := 0; n < b.N; n++ {
Debugf("foo bar %d", n)
}
}