-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzap_driver_test.go
108 lines (92 loc) · 2.47 KB
/
zap_driver_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
package slf4gozap
import (
"testing"
slog "github.com/go-eden/slf4go"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
var (
zapcfg = zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: false,
// DisableCaller: true,
DisableStacktrace: true,
Encoding: "console",
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
InitialFields: map[string]interface{}{"foo": "bar"},
}
cfg = Config{
ZapConfig: &zapcfg,
ZapOptions: []zap.Option{
zap.AddCallerSkip(SkipUntilTrueCaller),
},
}
)
// global logger
// go test --run GlobalLogger
func TestGlobalLogger(t *testing.T) {
Init(&cfg)
slog.Debug("global logger")
slog.Warnf("global logger, warnning: %v", "surrender")
}
// default logger
// go test --run DefaultLogger
func TestDefaultLogger(t *testing.T) {
Init(&cfg)
l := slog.GetLogger()
l.Errorf("default logger name=%s", l.Name())
}
// default logger
// go test --run BindFields
func TestBindFields(t *testing.T) {
Init(&cfg)
l := slog.GetLogger()
l.BindFields(slog.Fields{
"type": "default",
})
l.Errorf("default logger name=%s", l.Name())
}
// new logger use with
// go test --run LoggerGenByWith
func TestLoggerGenByWith(t *testing.T) {
Init(&cfg)
l := slog.GetLogger()
l.Infof("with logger name=%s", l.Name())
l2 := l.WithFields(slog.Fields{"type": "with1"})
l2.Infof("with logger name=%s", l2.Name())
l3 := l.WithFields(slog.Fields{"type": "with2"})
l3.Infof("with logger name=%s", l3.Name())
}
// logger generated by newLogger function
// go test --run LoggerGenByNew
func TestLoggerGenByNew(t *testing.T) {
Init(&cfg)
l1 := slog.NewLogger("NewLogger1")
l1.BindFields(slog.Fields{"type": "new1"})
l1.Infof("new logger name=%s", l1.Name())
l2 := slog.NewLogger("NewLogger2")
l2.BindFields(slog.Fields{"type": "new2"})
l2.Infof("new logger name=%s", l2.Name())
}
// logger generated by newLogger function
// go test -bench LoggerGenByWith
func BenchmarkLoggerGenByWith(b *testing.B) {
Init(&cfg)
l := slog.GetLogger()
l2 := l.WithFields(slog.Fields{"type": "with1"})
for i := 0; i < b.N; i++ {
l2.Infof("with logger name=%s", l2.Name())
}
}
func TestZaptest(t *testing.T) {
slog.SetDriver(&ZapDriver{
Logger: zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel)),
})
l := slog.GetLogger()
l.Info("info message")
if l.Level() != slog.InfoLevel {
t.Errorf("log level is misconfigured, got = %v, want = INFO", l.Level())
}
}