-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_test.go
206 lines (182 loc) · 7.82 KB
/
log_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
package log_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/pkg/errors"
"github.com/rockbears/log"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
fieldComponent = log.Field("component")
fieldAsset = log.Field("asset")
)
func init() {
registerDefaultFields()
}
func registerDefaultFields() {
log.RegisterField(fieldComponent, fieldAsset)
}
func ExampleNewLogrusWrapper() {
// Init the wrapper
log.Factory = log.NewLogrusWrapper(logrus.StandardLogger())
log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile)
// Init the logrus logger
logrus.StandardLogger().SetLevel(logrus.InfoLevel)
logrus.StandardLogger().SetFormatter(&logrus.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
})
logrus.StandardLogger().Out = os.Stdout
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleWithLogrus")
log.Debug(ctx, "this log should not be displayed")
log.Info(ctx, "this is %q", "info")
log.Warn(ctx, "this is warn")
log.Error(ctx, "this is error")
// Output:
// level=info msg="this is \"info\"" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log
// level=warning msg="this is warn" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log
// level=error msg="this is error" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log
}
func ExampleNewStdWrapper() {
// Init the wrapper
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true})
log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile)
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleNewStdWrapper")
log.Debug(ctx, "this log should not be displayed")
log.Info(ctx, "this is %q", "info")
log.Warn(ctx, "this is warn")
log.Error(ctx, "this is error")
// Output:
// [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is "info"
// [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is warn
// [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is error
}
func ExampleNewZapWrapper() {
// Init the wrapper
encoderCfg := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}
core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), os.Stdout, zap.InfoLevel)
log.Factory = log.NewZapWrapper(zap.New(core))
log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile)
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleNewZapWrapper")
log.Debug(ctx, "this log should not be displayed")
log.Info(ctx, "this is %q", "info")
log.Warn(ctx, "this is warn")
log.Error(ctx, "this is error")
// Output:
// {"level":"info","msg":"this is \"info\"","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"}
// {"level":"warn","msg":"this is warn","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"}
// {"level":"error","msg":"this is error","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"}
}
func TestErrorWithStackTrace(t *testing.T) {
// Init the wrapper
log.Factory = log.NewTestingWrapper(t)
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleErrorWithStackTrace")
log.ErrorWithStackTrace(ctx, fmt.Errorf("this is an error"))
log.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error")))
}
func TestErrorWithStackTraceAndFactory(t *testing.T) {
// Init the wrapper
logger := log.NewWithFactory(log.NewTestingWrapper(t))
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleErrorWithStackTrace")
logger.ErrorWithStackTrace(ctx, fmt.Errorf("this is an error"))
logger.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error")))
}
func ExampleNewStdWrapperAndSkip() {
// Init the wrapper
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true})
log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile)
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleNewStdWrapper")
log.Debug(ctx, "this log should not be displayed")
log.Info(ctx, "this is %q", "info")
log.Warn(ctx, "this is warn")
log.Error(ctx, "this is error")
log.Skip(fieldAsset, "ExampleNewStdWrapper")
log.Info(ctx, "this log should not be displayed because is should be skipped")
// Output:
// [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is "info"
// [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is warn
// [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is error
}
func ExampleGetRegisteredFields() {
log.RegisterDefaultFields()
registerDefaultFields()
fmt.Println(log.GetRegisteredFields())
// Output:
// [asset caller component source_file source_line stack_trace]
}
func TestGetRegisteredFields(t *testing.T) {
log.RegisterDefaultFields()
defer log.RegisterDefaultFields()
registerDefaultFields()
defer registerDefaultFields()
log.UnregisterField(log.GetRegisteredFields()...)
got := log.GetRegisteredFields()
if len(got) != 0 {
t.Fatalf("want empty slice, got %s", got)
}
log.RegisterField(log.FieldCaller)
got = log.GetRegisteredFields()
if len(got) != 1 {
t.Fatalf("want slice with length 1, got %s", got)
}
gotField := got[0]
wantField := log.FieldCaller
if gotField != wantField {
t.Fatalf("want field %s, got %s", wantField, gotField)
}
}
func ExampleNewWithFactory() {
// Init the wrapper
lrus := logrus.New()
logger := log.NewWithFactory(log.NewLogrusWrapper(lrus))
logger.RegisterField(fieldComponent, fieldAsset)
logger.UnregisterField(log.FieldSourceLine, log.FieldSourceFile)
// Init the logrus logger
lrus.SetLevel(logrus.InfoLevel)
lrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
DisableTimestamp: true,
})
lrus.Out = os.Stdout
// Init the context
ctx := context.Background()
ctx = context.WithValue(ctx, fieldComponent, "rockbears/log")
ctx = context.WithValue(ctx, fieldAsset, "ExampleWithLogrus")
logger.Debug(ctx, "this log should not be displayed")
logger.Info(ctx, "this is %q", "info")
logger.Warn(ctx, "this is warn")
logger.Error(ctx, "this is error")
// Output:
// level=info msg="this is \"info\"" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewWithFactory component=rockbears/log
// level=warning msg="this is warn" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewWithFactory component=rockbears/log
// level=error msg="this is error" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewWithFactory component=rockbears/log
}