This repository has been archived by the owner on May 8, 2024. It is now read-only.
generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
119 lines (103 loc) · 2.46 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
package log_test
import (
"context"
"testing"
"github.com/things-go/log"
)
func init() {
l, lv := log.New(log.WithConfig(log.Config{Level: "debug", Format: "json"}))
log.ReplaceGlobals(log.NewLoggerWith(l, lv))
log.SetDefaultValuer(
log.Caller(3),
func(ctx context.Context) log.Field {
return log.String("deft_key1", "deft_val1")
},
)
}
func Test_LoggerNormal(t *testing.T) {
log.Debug("Debug")
log.Info("Info")
log.Warn("Warn")
log.Error("Error")
log.DPanic("DPanic")
// no hook
l := log.WithNewValuer()
l.Debug("Debug")
l.Info("Info")
l.Warn("Warn")
l.Error("Error")
l.DPanic("DPanic")
}
func Test_LoggerFormater(t *testing.T) {
log.Debugf("Debugf: %s", "debug")
log.Infof("Infof: %s", "info")
log.Warnf("Warnf: %s", "warn")
log.Errorf("Errorf: %s", "error")
log.DPanicf("DPanicf: %s", "dPanic")
// no hook
l := log.WithNewValuer()
l.Debugf("Debugf: %s", "debug")
l.Infof("Infof: %s", "info")
l.Warnf("Warnf: %s", "warn")
l.Errorf("Errorf: %s", "error")
l.DPanicf("DPanicf: %s", "dPanic")
}
func Test_LoggerKeyValue(t *testing.T) {
log.Debugw("Debugw", "Debugw", "w")
log.Infow("Infow", "Infow", "w")
log.Warnw("Warnw", "Warnw", "w")
log.Infow("Infow", "Infow", "w")
log.Errorw("Errorw", "Errorw", "w")
log.DPanicw("DPanicw", "DPanicw", "w")
}
func TestPanic(t *testing.T) {
shouldPanic(t, func() {
log.Panic("Panic")
})
shouldPanic(t, func() {
log.Panicf("Panicf: %s", "panic")
})
shouldPanic(t, func() {
log.Panicw("Panicw: %s", "panic", "w")
})
}
func Test_LoggerWith(t *testing.T) {
log.With(
log.String("string", "bb"),
log.Int16("int16", 100),
).
Debug("debug with")
}
func Test_LoggerNamed(t *testing.T) {
log.Named("another").Debug("debug named")
}
func Test_Logger_ZapLogger(t *testing.T) {
log.Logger().Debug("desugar")
}
func Test_LoggerNamespace(t *testing.T) {
log.Logger().With(log.Namespace("aaaa")).With(log.String("xx", "yy"), log.String("aa", "bb")).Debug("with namespace")
_ = log.Sync()
}
type ctxKey struct{}
func Test_Logger_WithContext(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxKey{}, "ctx_value")
ctxValuer := func(ctx context.Context) log.Field {
s, ok := ctx.Value(ctxKey{}).(string)
if !ok {
return log.Skip()
}
return log.String("ctx_key", s)
}
log.WithContext(ctx).
WithValuer(ctxValuer).
Debug("with context")
}
func shouldPanic(t *testing.T, f func()) {
defer func() {
e := recover()
if e == nil {
t.Errorf("should panic but not")
}
}()
f()
}