-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
303 lines (254 loc) · 8.32 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
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
package log
/*
Copyright 2019 Bruno Moura <[email protected]>
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.
*/
import (
"bytes"
"errors"
"io/ioutil"
"os"
"testing"
)
func TestLogEntry(t *testing.T) {
config := DefaultConfig
config.EnableTime = false
config.EnableCaller = false
config.Level = DEBUG
l := New(nil, config)
l = l.Hooks(func(e Entry) {
switch e.Level() {
case DEBUG:
w := []byte(`{"level":"debug", "message":"debug message", "string value":"text", "int value":8, "null value":null, "error":"new error"}`)
if !bytes.Equal(w, e.Bytes()) {
t.Fatal("error logging debug")
}
case INFO:
w := []byte(`{"level":"info", "message":"info message", "string value":"text", "int value":8, "null value":null, "error":"new error"}`)
if !bytes.Equal(w, e.Bytes()) {
t.Fatal("error logging info")
}
case WARN:
w := []byte(`{"level":"warn", "message":"warn message", "string value":"text", "int value":8, "null value":null, "error":"new error"}`)
if !bytes.Equal(w, e.Bytes()) {
t.Fatal("error logging warn")
}
case ERROR:
w := []byte(`{"level":"error", "message":"error message", "string value":"text", "int value":8, "null value":null, "error":"new error"}`)
if !bytes.Equal(w, e.Bytes()) {
t.Fatal("error logging error")
}
}
})
l.Debug("debug message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.Info("info message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.Warn("warn message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.Error("error message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
}
func TestLogSetFormat(t *testing.T) {
config := DefaultConfig
config.EnableTime = false
config.EnableCaller = false
config.Level = DEBUG
l := New(nil, config)
jsonCount := 0
textCount := 0
l = l.Hooks(func(e Entry) {
switch e.o.enc.format {
case FormatJSON:
jsonCount++
case FormatText:
textCount++
}
})
l.Debug("debug message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.SetFormat(FormatText)
l.Info("info message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.SetFormat(FormatJSON)
l.Info("info message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
if jsonCount != 2 || textCount != 1 {
t.Error("invalid count for each format")
}
}
func TestLogSetLevel(t *testing.T) {
config := DefaultConfig
config.EnableTime = false
config.EnableCaller = false
l := New(nil, config)
infoCount := 0
debugCount := 0
l = l.Hooks(func(e Entry) {
switch e.Level() {
case INFO:
infoCount++
case DEBUG:
debugCount++
}
})
l.SetLevel(INFO)
l.Debug("debug message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.SetLevel(DEBUG)
l.Debug("debug message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.Info("info message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
l.SetLevel(ERROR)
l.Info("info message").
String("string value", "text").
Int("int value", 8).Null("null value").
Error("error", errors.New("new error")).Write()
if infoCount != 1 || debugCount != 1 {
t.Error("invalid count for level", infoCount, debugCount)
}
}
func TestLogText(t *testing.T) {
config := DefaultConfig
config.EnableTime = false
config.EnableCaller = false
config.Level = DEBUG
config.Format = FormatText
l := New(nil, config)
l.Hooks(func(e Entry) {
w := []byte(`level="debug" message="debug message" string="text" int=8 null=null error="new error"`)
if !bytes.Equal(w, e.Bytes()) {
t.Fatal("error logging warn")
}
})
l.Debug("debug message").
String("string", "text").
Int("int", 8).Null("null").
Error("error", errors.New("new error")).Write()
}
func TestLogSampler(t *testing.T) {
logCount := 10000
w := &writerCounter{}
config := DefaultConfig
l := New(w, config)
for x := 0; x < logCount; x++ {
l.Error("error message").
String("string value", "text").
Int("int value", 8).
Write()
}
if logCount <= w.count {
t.Fatalf("number of interactions %d number of writes %d", logCount, w.count)
}
}
func BenchmarkLogNoSampling(b *testing.B) {
config := DefaultConfig
config.Level = DEBUG
config.EnableCaller = false
config.EnableSampling = false
l := New(ioutil.Discard, config)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l.Info("informational message").
String("string value", "text").
Int("int value", 8).Float64("float", 722727272.0099).
Int("int", 8).Float64("float value", 722727272.0099).
Write()
}
}
func BenchmarkLogWithSampling(b *testing.B) {
config := DefaultConfig
config.Level = DEBUG
config.EnableCaller = false
config.EnableSampling = true
l := New(ioutil.Discard, config)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l.Info("informational message").
String("string value", "text").
Int("int value", 8).Float64("float", 722727272.0099).
Int("int", 8).Float64("float value", 722727272.0099).
Write()
}
}
func BenchmarkLogNoLevel(b *testing.B) {
config := DefaultConfig
config.Level = ERROR
config.EnableCaller = false
l := New(os.Stdout, config)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l.Info("informational message").
String("string value", "text").
Int("int value", 8).Float64("float", 722727272.0099).
Int("int", 8).Float64("float value", 722727272.0099).
Write()
}
}
func Example() {
config := DefaultConfig
config.Level = DEBUG
config.Format = FormatText // Enable text logging
// New logger with added context
l := New(os.Stdout, config).
With(func(e Entry) {
e.String("app", "app1")
})
// Simple logging
l.Info("info message").String("key", "value").Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="info" caller="_local/main.go:26" app="app1" message="info message" key="value"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"info", "caller":"_local/main.go:26", "app":"app1", "message":"info message", "key":"value"}
l.Warn("warn message").Bool("flag", false).Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="warn" caller="_local/main.go:29" app="app1" message="warn message" flag=false
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"warn", "caller":"_local/main.go:29", "app":"app1", "message":"warn message", "flag":false}
l.Error("caught an error").String("error", "request error").Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="error" caller="_local/main.go:32" app="app1" message="caught an error" error="request error"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"error", "caller":"_local/main.go:32", "app":"app1", "message":"caught an error", "error":"request error"}
l.Fatal("caught an unrecoverable error").Error("error", errors.New("some error")).Write()
// Text format:
// time="2021-03-25T13:32:50.391Z" level="fatal" caller="_local/main.go:35" app="app1" message="caught an unrecoverable error" error="some error"
// JSON format:
// {"time":"2021-03-25T13:33:20.547Z", "level":"fatal", "caller":"_local/main.go:35", "app":"app1", "message":"caught an unrecoverable error", "error":"some error"}
}
type writerCounter struct {
count int
}
func (w *writerCounter) Write(p []byte) (n int, err error) {
w.count++
return len(p), nil
}