-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger_test.go
471 lines (390 loc) · 11.7 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package noodlog
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
"testing"
)
var errorFmt string = "%s failed: expected %v, got %v"
var defaultLogger = Logger{
level: infoLevel,
logWriter: os.Stdout,
prettyPrint: false,
traceCaller: false,
traceCallerLevel: 5,
obscureSensitiveData: false,
sensitiveParams: nil,
colors: false,
colorMap: colorMap,
}
var customLogger = Logger{
level: errorLevel,
logWriter: os.Stderr,
prettyPrint: true,
traceCaller: true,
traceCallerLevel: 6,
obscureSensitiveData: true,
sensitiveParams: []string{"password"},
colors: true,
colorMap: colorMap,
}
func toStr(obj interface{}) string {
return fmt.Sprintf("%v", obj)
}
func wildCardToRegexp(pattern string) string {
var result strings.Builder
for i, literal := range strings.Split(pattern, "*") {
// Replace * with .*
if i > 0 {
result.WriteString(".*")
}
// Quote any regular expression meta characters in the literal text.
result.WriteString(regexp.QuoteMeta(literal))
}
return result.String()
}
func Matches(value string, pattern string) bool {
result, _ := regexp.MatchString(wildCardToRegexp(pattern), value)
return result
}
func TestNewLogger(t *testing.T) {
expected := toStr(defaultLogger)
actual := toStr(*NewLogger())
if actual != expected {
t.Errorf(errorFmt, "TestNewLogger", expected, actual)
}
}
func TestSetConfigsEmptyConfigs(t *testing.T) {
expected := toStr(defaultLogger)
actual := toStr(*NewLogger().SetConfigs(Configs{}))
if actual != expected {
t.Errorf(errorFmt, "TestSetConfigsEmptyConfigs", expected, actual)
}
}
func TestSetConfigsFullConfigsAllEnabled(t *testing.T) {
expected := toStr(customLogger)
actual := toStr(*NewLogger().SetConfigs(Configs{
LogLevel: LevelError,
LogWriter: os.Stderr,
JSONPrettyPrint: Enable,
TraceCaller: Enable,
SinglePointTracing: Enable,
Colors: Enable,
CustomColors: &CustomColors{Trace: Color{}, Debug: Green},
ObscureSensitiveData: Enable,
SensitiveParams: []string{"password"},
}))
if actual != expected {
t.Errorf(errorFmt, "TestSetConfigsFullConfigsAllEnabled", expected, actual)
}
}
func TestSetConfigsFullConfigsAllDisabled(t *testing.T) {
customLogger.obscureSensitiveData = false
customLogger.prettyPrint = false
customLogger.colors = false
customLogger.traceCaller = false
customLogger.traceCallerLevel = 5
customLogger.obscureSensitiveData = false
expected := toStr(customLogger)
actual := toStr(*NewLogger().SetConfigs(Configs{
LogLevel: LevelError,
LogWriter: os.Stderr,
JSONPrettyPrint: Disable,
TraceCaller: Disable,
SinglePointTracing: Disable,
Colors: Disable,
ObscureSensitiveData: Disable,
SensitiveParams: []string{"password"},
}))
if actual != expected {
t.Errorf(errorFmt, "TestSetConfigsFullConfigsAllDisabled", expected, actual)
}
}
func TestLevel(t *testing.T) {
l := NewLogger()
testMap := map[string]int{
traceLabel: traceLevel,
infoLabel: infoLevel,
warnLabel: warnLevel,
errorLabel: errorLevel,
panicLabel: panicLevel,
fatalLabel: fatalLevel,
}
for input, expected := range testMap {
if l.Level(input).level != expected {
t.Errorf(errorFmt, "TestLevel", expected, l.level)
}
}
}
func TestLogWriter(t *testing.T) {
l := NewLogger()
testSlice := []io.Writer{
os.Stdin,
os.Stdout,
os.Stderr,
}
for _, expected := range testSlice {
if l.LogWriter(expected).logWriter != expected {
t.Errorf(errorFmt, "TestLogWriter", expected, l.logWriter)
}
}
}
func TestEnableDisableJSONPrettyPrint(t *testing.T) {
l := NewLogger()
errFormat := "TestEnableDisableJSONPrettyPrint failed: expected prettyPrint %t, got %t"
if !l.EnableJSONPrettyPrint().prettyPrint {
t.Errorf(errFormat, true, l.prettyPrint)
}
if l.DisableJSONPrettyPrint().prettyPrint {
t.Errorf(errFormat, false, l.prettyPrint)
}
}
func TestEnableDisableTraceCaller(t *testing.T) {
l := NewLogger()
errFormat := "TestEnableDisableTraceCaller failed: expected traceCaller %t, got %t"
if !l.EnableTraceCaller().traceCaller {
t.Errorf(errFormat, true, l.traceCaller)
}
if l.DisableTraceCaller().traceCaller {
t.Errorf(errFormat, false, l.traceCaller)
}
}
func TestEnableDisableSinglePointTracing(t *testing.T) {
l := NewLogger()
errFormat := "TestEnableDisableSinglePointTracing failed: expected traceCallerLevel %d, got %d"
if l.EnableSinglePointTracing().traceCallerLevel != 6 {
t.Errorf(errFormat, 6, l.traceCallerLevel)
}
if l.DisableSinglePointTracing().traceCallerLevel != 5 {
t.Errorf(errFormat, 5, l.traceCallerLevel)
}
}
func TestEnableDisableLoggerColors(t *testing.T) {
l := NewLogger()
errFormat := "TestEnableDisableLoggerColors failed: expected colors %t, got %t"
if !l.EnableColors().colors {
t.Errorf(errFormat, true, l.colors)
}
if l.DisableColors().colors {
t.Errorf(errFormat, false, l.colors)
}
}
func TestEnableDisableObscureSensitiveParams(t *testing.T) {
params := []string{"password", "age"}
errFormat := "TestEnableDisableObscureSensitiveParams failed: expected obscureSensitiveData %t, got %t"
l := NewLogger()
l.EnableObscureSensitiveData(params)
if !l.obscureSensitiveData {
t.Errorf(errFormat, true, l.obscureSensitiveData)
}
if !reflect.DeepEqual(l.sensitiveParams, params) {
t.Errorf("TestEnableDisableObscureSensitiveParams failed: expected sensitiveParams %v. got %v", params, l.sensitiveParams)
}
l.DisableObscureSensitiveData()
if l.obscureSensitiveData {
t.Errorf(errFormat, false, l.obscureSensitiveData)
}
}
func TestSetSensitiveParams(t *testing.T) {
params := []string{"secret", "privatekey"}
errFormat := "TestEnableDisableObscureSensitiveParams failed: expected sensitiveParams %v. got %v"
l := NewLogger()
l.EnableObscureSensitiveData(params)
if !reflect.DeepEqual(l.SetSensitiveParams(params).sensitiveParams, params) {
t.Errorf(errFormat, params, l.sensitiveParams)
}
}
type account struct {
Username string `json:"username"`
Password string `json:"password"`
}
func TestInfoLogging(t *testing.T) {
var testLoggingMap = map[interface{}]string{
"": `"level":"%s","message":""`,
"hello": `"level":"%s","message":"hello"`,
`{"name": "gyoza", "cool": true, "password": "Sup3rS3cr3t"}`: `"level":"%s","message":{"cool":true,"name":"gyoza","password":"**********"}`,
"{\"name\": \"gyozatech\", \"repo\": \"noodlog\"}": `"level":"%s","message":{"name":"gyozatech","repo":"noodlog"}`,
account{"gyozatech", "Sup3rS3cr3t"}: `"level":"%s","message":{"password":"**********","username":"gyozatech"}`,
}
var b bytes.Buffer
b.Reset()
l := NewLogger().EnableObscureSensitiveData([]string{"password"}).LogWriter(&b)
for input, expectedFmt := range testLoggingMap {
if input != "" {
l.Info(input)
} else {
l.Info()
}
actual := b.String()
expected := fmt.Sprintf(expectedFmt, "info")
if !strings.Contains(actual, expected) {
t.Errorf(errorFmt, "TestInfoLogging", expected, actual)
}
b.Reset()
}
}
func TestLogging(t *testing.T) {
var b bytes.Buffer
b.Reset()
l := NewLogger().
EnableObscureSensitiveData([]string{"password"}).
EnableTraceCaller().
EnableColors().
LogWriter(&b)
for _, level := range []string{"trace", "debug", "info", "warn", "error"} {
input1 := "logging example"
input2 := "text"
input3 := "logging example %s"
input4 := "logging example text"
expected := `{"level":"*","message":"logging example text","time":"*"}`
switch level {
case "trace":
l.Trace(input1, input2)
case "debug":
l.Debug(input1, input2)
case "info":
l.Info(input1, input2)
case "warn":
l.DisableObscureSensitiveData()
l.Warn(input4)
case "error":
l.Error(input3, input2)
}
actual := b.String()
if level == "debug" || level == "trace" {
if actual != "" {
t.Errorf(errorFmt, "TestLogging", "", actual)
}
} else {
if !Matches(actual, expected) {
t.Errorf(errorFmt, "TestLogging", expected, actual)
}
}
b.Reset()
}
}
func TestLogPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf(errorFmt, "TestLogPanic", "panic", "not-panic")
}
}()
l := NewLogger()
l.Panic("Hello, I'm gonna panic!")
}
func TestLogFatal(t *testing.T) {
var b bytes.Buffer
b.Reset()
l := NewLogger().EnableJSONPrettyPrint().LogWriter(&b)
os.Setenv("EXIT_ON_FATAL_DISABLED", "true")
l.Fatal("Hello, I'm gonna exit!")
actual := b.String()
expected := `{
"level": "fatal",
"message": "Hello, I'm gonna exit!",
"time": "*"
}`
b.Reset()
if Matches(actual, expected) {
t.Errorf(errorFmt, "TestLogFatal", expected, actual)
}
}
func composeColor(color string) string {
return "\033[" + color + "m"
}
func TestSetCustomColors(t *testing.T) {
errFormat := "TestSetCustomColors failed: expected %s got %s"
l := NewLogger()
l.SetCustomColors(CustomColors{
Trace: Blue,
Debug: Purple,
Info: Yellow,
Warn: Green,
Error: Default,
})
if blueCode := composeColor(colorBlue); l.colorMap[traceLabel] != blueCode {
t.Errorf(errFormat, blueCode, l.colorMap[traceLabel])
}
if purpleCode := composeColor(colorPurple); l.colorMap[debugLabel] != purpleCode {
t.Errorf(errFormat, purpleCode, l.colorMap[debugLabel])
}
if yellowCode := composeColor(colorYellow); l.colorMap[infoLabel] != yellowCode {
t.Errorf(errFormat, yellowCode, l.colorMap[infoLabel])
}
if greenCode := composeColor(colorGreen); l.colorMap[warnLabel] != greenCode {
t.Errorf(errFormat, greenCode, l.colorMap[warnLabel])
}
if defaultCode := composeColor(colorReset); l.colorMap[errorLabel] != defaultCode {
t.Errorf(errFormat, defaultCode, l.colorMap[errorLabel])
}
}
var colorTestMap = map[Color]string{
NewColor(Blue): composeColor(colorBlue),
NewColor(Purple): composeColor(colorPurple),
}
func TestSetTraceColor(t *testing.T) {
l := NewLogger()
for color, colorCode := range colorTestMap {
l.SetTraceColor(color)
if actualCode := l.colorMap[traceLabel]; actualCode != colorCode {
t.Errorf(errorFmt, "TestSetTraceColor", colorCode, actualCode)
}
}
}
func TestSetDebugColor(t *testing.T) {
l := NewLogger()
for color, colorCode := range colorTestMap {
l.SetDebugColor(color)
if actualCode := l.colorMap[debugLabel]; actualCode != colorCode {
t.Errorf(errorFmt, "TestSetDebugColor", colorCode, actualCode)
}
}
}
func TestSetInfoColor(t *testing.T) {
l := NewLogger()
for color, colorCode := range colorTestMap {
l.SetInfoColor(color)
if actualCode := l.colorMap[infoLabel]; actualCode != colorCode {
t.Errorf(errorFmt, "TestSetInfoColor", colorCode, actualCode)
}
}
}
func TestSetWarnColor(t *testing.T) {
l := NewLogger()
for color, colorCode := range colorTestMap {
l.SetWarnColor(color)
if actualCode := l.colorMap[warnLabel]; actualCode != colorCode {
t.Errorf(errorFmt, "TestSetWarnColor", colorCode, actualCode)
}
}
}
func TestSetErrorColor(t *testing.T) {
l := NewLogger()
for color, colorCode := range colorTestMap {
l.SetErrorColor(color)
if actualCode := l.colorMap[errorLabel]; actualCode != colorCode {
t.Errorf(errorFmt, "TestSetErrorColor", colorCode, actualCode)
}
}
}
func TestAdaptMessage(t *testing.T) {
testMap := map[interface{}]string{
struct{ test string }{"Hello test"}: "{Hello test}",
"Hi message": "Hi message",
`{"name": "John", "surname": "Doe"}`: `map[name:John surname:Doe]`,
fmt.Errorf("Nice error!"): "Nice error!",
}
var b bytes.Buffer
b.Reset()
l := NewLogger().LogWriter(&b)
for input, expected := range testMap {
actual := l.adaptMessage(input)
if toStr(actual) != expected {
t.Errorf(errorFmt, "TestAdaptMessage", expected, actual)
}
}
}