-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathlogger_categorized_test.go
More file actions
83 lines (73 loc) · 2.23 KB
/
logger_categorized_test.go
File metadata and controls
83 lines (73 loc) · 2.23 KB
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
package log
import (
"bytes"
"io"
"strings"
"testing"
)
func TestLoggerCategorizedLogLevels(t *testing.T) {
var b bytes.Buffer
logger := Logger{Level: DebugLevel, Writer: &IOWriter{Writer: &b}}
// Categorized logger must be indepented of base logger
cat1Logger := logger.Categorized("cat1")
cat1Logger.SetLevel(TraceLevel)
logger.Debug().Msg("logger debug here")
cat1Logger.Debug().Msg("cat1Logger debug here")
if !strings.Contains(b.String(), `"logger debug here"`) {
t.Fatal("logger.Debug must be logged")
}
if !strings.Contains(b.String(), `"cat1Logger debug here"`) {
t.Fatal("cat1Logger.Debug must be logged for ")
}
if !strings.Contains(b.String(), `"category":"cat1"`) {
t.Fatal("cat1Logger.Debug is missing category")
}
// Changing loglevel on category must not change base logger
b.Reset()
cat1Logger.SetLevel(InfoLevel)
logger.Debug().Msg("logger debug here")
cat1Logger.Debug().Msg("cat1Logger debug here")
if !strings.Contains(b.String(), `"logger debug here"`) {
t.Fatal("logger.Debug must be logged")
}
if strings.Contains(b.String(), `"cat1Logger debug here"`) {
t.Fatal("cat1Logger.Debug must be logged")
}
// Loglevel on other categorized logger has own loglevel
b.Reset()
cat2Logger := logger.Categorized("cat2")
cat2Logger.SetLevel(TraceLevel)
cat2Logger.Trace().Msg("cat2Logger trace here")
if !strings.Contains(b.String(), `"cat2Logger trace here"`) {
t.Fatal("logger.Debug for category cat2 must be logged")
}
if !strings.Contains(b.String(), `"category":"cat2"`) {
t.Fatal("cat2Logger.Debug is missing category")
}
}
func BenchmarkCategorizedLogger(b *testing.B) {
logger := Logger{
TimeFormat: TimeFormatUnix,
Level: DebugLevel,
Writer: IOWriter{io.Discard},
}
catLogger := logger.Categorized("one")
catLogger.SetLevel(DebugLevel)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
catLogger.Debug().Str("foo", "bar").Msgf("hello %s", "world")
}
}
func BenchmarkCategorizedLoggerBadUsage(b *testing.B) {
logger := Logger{
TimeFormat: TimeFormatUnix,
Level: DebugLevel,
Writer: IOWriter{io.Discard},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Categorized("one").Info().Str("foo", "bar").Msgf("hello %s", "world")
}
}