-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjlo_test.go
585 lines (520 loc) · 13.9 KB
/
jlo_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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package jlo_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/dcmn-com/jlo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testTime = "2018-08-02T21:48:56.856339554Z"
type LogFunc func(string, ...interface{})
func TestMain(m *testing.M) {
jlo.Now = func() time.Time {
t, err := time.Parse(time.RFC3339Nano, testTime)
if err != nil {
panic(err)
}
return t
}
os.Exit(m.Run())
}
func Test_SetLogLevel(t *testing.T) {
jlo.SetLogLevel(jlo.InfoLevel)
buf := bytes.NewBuffer(nil)
infoLogger := jlo.NewLogger(buf)
infoLogger.Debugf("should not log")
assert.Empty(t, buf.String())
jlo.SetLogLevel(jlo.DebugLevel)
debugLogger := jlo.NewLogger(buf)
debugLogger.Debugf("should log")
var msg map[string]interface{}
err := json.NewDecoder(buf).Decode(&msg)
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{
"@level": "debug",
"@message": "should log",
"@timestamp": msg["@timestamp"],
}, msg)
}
func Test_Logger_Debugf(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"simple": {
String: "I'm real",
Message: "I'm real",
},
"with format args": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{"I'm real", 5, 0.1, true},
Message: "string: I'm real int: 5 float: 0.10 bool: true",
},
"message including double brackets": {
String: `I'm "real"`,
Message: `I'm \"real\"`,
},
"message arg including double brackets": {
String: "I'm %s",
Args: []interface{}{`"real"`},
Message: `I'm \"real\"`,
},
"message args including double brackets": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{`"I'm real"`, 5, 0.1, true},
Message: `string: \"I'm real\" int: 5 float: 0.10 bool: true`,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(jlo.DebugLevel)
l.Debugf(test.String, test.Args...)
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "debug",
"@timestamp": "%s"
}`, test.Message, testTime), buf.String())
})
}
}
func Test_Logger_Infof(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"simple": {
String: "I'm real",
Message: "I'm real",
},
"with format args": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{"I'm real", 5, 0.1, true},
Message: "string: I'm real int: 5 float: 0.10 bool: true",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(jlo.InfoLevel)
l.Infof(test.String, test.Args...)
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "info",
"@timestamp": "%s"
}`, test.Message, testTime), buf.String())
})
}
}
func Test_Logger_Warnf(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"simple": {
String: "I'm real",
Message: "I'm real",
},
"with format args": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{"I'm real", 5, 0.1, true},
Message: "string: I'm real int: 5 float: 0.10 bool: true",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(jlo.WarningLevel)
l.Warnf(test.String, test.Args...)
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "warning",
"@timestamp": "%s"
}`, test.Message, testTime), buf.String())
})
}
}
func Test_Logger_Errorf(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"simple": {
String: "I'm real",
Message: "I'm real",
},
"with format args": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{"I'm real", 5, 0.1, true},
Message: "string: I'm real int: 5 float: 0.10 bool: true",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(jlo.InfoLevel)
l.Errorf(test.String, test.Args...)
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "error",
"@timestamp": "%s"
}`, test.Message, testTime), buf.String())
})
}
}
func Test_Logger_Fatalf(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"simple": {
String: "I'm real",
Message: "I'm real",
},
"with format args": {
String: "string: %s int: %d float: %.2f bool: %t",
Args: []interface{}{"I'm real", 5, 0.1, true},
Message: "string: I'm real int: 5 float: 0.10 bool: true",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(jlo.FatalLevel)
l.Fatalf(test.String, test.Args...)
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "fatal",
"@timestamp": "%s"
}`, test.Message, testTime), buf.String())
})
}
}
func Test_Logger_Infof_EnsureNewlineDelimitedJSON(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.Infof("I'm real")
assert.True(t, strings.HasSuffix(buf.String(), "}\n"))
}
func Test_Logger_Infof_SpecialCharacterUse(t *testing.T) {
tests := map[string]struct {
String string
Args []interface{}
Message string
}{
"double quotes": {
String: `"I'm" %s`,
Args: []interface{}{`"real"`},
Message: `"I'm" "real"`,
},
"backspace": {
String: "I'm\b %s",
Args: []interface{}{"real\b"},
Message: "I'm\b real\b",
},
"form feed": {
String: "I'm\f %s",
Args: []interface{}{"real\f"},
Message: "I'm\f real\f",
},
"new line": {
String: "I'm\n %s",
Args: []interface{}{"real\n"},
Message: "I'm\n real\n",
},
"carriage return": {
String: "I'm\r %s",
Args: []interface{}{"real\r"},
Message: "I'm\r real\r",
},
"tab": {
String: "I'm\t %s",
Args: []interface{}{"real\t"},
Message: "I'm\t real\t",
},
"backslash": {
String: "I'm\\ %s",
Args: []interface{}{"real\\"},
Message: "I'm\\ real\\",
},
"all": {
String: `"` + " \b \f \n \r \t \\ %s",
Args: []interface{}{`"` + " \b \f \n \r \t \\"},
Message: `"` + " \b \f \n \r \t \\ " + `"` + " \b \f \n \r \t \\",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.Infof(test.String, test.Args...)
var decoded map[string]string
err := json.Unmarshal(buf.Bytes(), &decoded)
if err != nil {
t.Logf("log destination data: '%s'", buf.String())
t.Fatal("error unmarshaling log destination data")
}
assert.Equal(t, map[string]string{
"@message": test.Message,
"@level": "info",
"@timestamp": testTime,
}, decoded)
})
}
}
func Test_Logger_WithField(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.WithField("@request_id", "e44c2a9").
WithField("@version", 2.1).
WithField("@revision", 6).
Infof("I'm real")
// check that field is in logs
assert.JSONEq(t, fmt.Sprintf(`{
"@level": "info",
"@message": "I'm real",
"@timestamp": "%s",
"@request_id": "e44c2a9",
"@revision": 6,
"@version": 2.1
}`, testTime), buf.String())
// check that original logger is unaffected
buf.Reset()
l.Infof("I'm real")
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "I'm real",
"@level": "info",
"@timestamp": "%s"
}`, testTime), buf.String())
}
func Test_Logger_WithField_EnsureNoChangeWhenIgnoringReturnValue(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.WithField("WithField", "will only be set in returned logger")
l.Infof("I'm real")
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "I'm real",
"@level": "info",
"@timestamp": "%s"
}`, testTime), buf.String())
}
func Test_Logger_SetLogLevel(t *testing.T) {
tests := map[string]struct {
GetLogFunc func(l *jlo.Logger) LogFunc
LogLevel jlo.LogLevel
SetLogLevel jlo.LogLevel
Message string
}{
"Debug log with DebugLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Debugf
},
LogLevel: jlo.DebugLevel,
SetLogLevel: jlo.DebugLevel,
Message: "I'm real",
},
"Debug log with InfoLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Debugf
},
LogLevel: jlo.DebugLevel,
SetLogLevel: jlo.InfoLevel,
Message: "",
},
"Debug log with WarningLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Debugf
},
LogLevel: jlo.DebugLevel,
SetLogLevel: jlo.WarningLevel,
Message: "",
},
"Debug log with ErrorLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Debugf
},
LogLevel: jlo.DebugLevel,
SetLogLevel: jlo.ErrorLevel,
Message: "",
},
"Info log with DebugLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Infof
},
LogLevel: jlo.InfoLevel,
SetLogLevel: jlo.DebugLevel,
Message: "I'm real",
},
"Info log with InfoLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Infof
},
LogLevel: jlo.InfoLevel,
SetLogLevel: jlo.InfoLevel,
Message: "I'm real",
},
"Info log with WarningLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Infof
},
SetLogLevel: jlo.WarningLevel,
Message: "",
},
"Info log with ErrorLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Infof
},
LogLevel: jlo.InfoLevel,
SetLogLevel: jlo.ErrorLevel,
Message: "",
},
"Warning log with DebugLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Warnf
},
LogLevel: jlo.WarningLevel,
SetLogLevel: jlo.DebugLevel,
Message: "I'm real",
},
"Warning log with InfoLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Warnf
},
LogLevel: jlo.WarningLevel,
SetLogLevel: jlo.InfoLevel,
Message: "I'm real",
},
"Warning log with WarningLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Warnf
},
LogLevel: jlo.WarningLevel,
SetLogLevel: jlo.WarningLevel,
Message: "I'm real",
},
"Warning log with ErrorLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Warnf
},
LogLevel: jlo.WarningLevel,
SetLogLevel: jlo.ErrorLevel,
Message: "",
},
"Error log with DebugLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Errorf
},
LogLevel: jlo.ErrorLevel,
SetLogLevel: jlo.DebugLevel,
Message: "I'm real",
},
"Error log with InfoLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Errorf
},
LogLevel: jlo.ErrorLevel,
SetLogLevel: jlo.InfoLevel,
Message: "I'm real",
},
"Error log with WarningLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Errorf
},
LogLevel: jlo.ErrorLevel,
SetLogLevel: jlo.WarningLevel,
Message: "I'm real",
},
"Error log with ErrorLevel": {
GetLogFunc: func(l *jlo.Logger) LogFunc {
return l.Errorf
},
LogLevel: jlo.ErrorLevel,
SetLogLevel: jlo.ErrorLevel,
Message: "I'm real",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
l := jlo.NewLogger(buf)
l.SetLogLevel(test.SetLogLevel)
test.GetLogFunc(l)("I'm real")
if test.Message == "" {
if buf.Bytes() != nil {
t.Error("Expected log destination data to be empty")
}
return
}
assert.JSONEq(t, fmt.Sprintf(`{
"@message": "%s",
"@level": "%s",
"@timestamp": "%s"
}`, test.Message, test.LogLevel.String(), testTime), buf.String())
})
}
}
const (
testStringShort = "this is a short log string"
testStringLong = `Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Proin in tellus sit amet nibh dignissim sagittis. In convallis. Etiam ligula
pede, sagittis quis, interdum ultricies, scelerisque eu. Donec iaculis gravida
nulla. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Nunc tincidunt ante vitae massa. Ut enim ad
minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam,
nisi ut aliquid ex ea commodi consequatur? Vestibulum erat nulla, ullamcorper
nec, rutrum non, nonummy ac, erat. Curabitur bibendum justo non orci. Etiam
sapien elit, consequat eget, tristique non, venenatis quis, ante. Duis pulvinar.
Aliquam erat volutpat. In convallis. Vestibulum fermentum tortor id mi.`
)
func Benchmark_Logger_ShortString(b *testing.B) {
benchmarkLogger(b, testStringShort)
}
func Benchmark_Logger_ShortString_WithArgs(b *testing.B) {
benchmarkLogger(b, testStringShort+"%s", "I'm real")
}
func Benchmark_Logger_ShortString_WithField(b *testing.B) {
benchmarkLoggerWithField(b, testStringShort)
}
func Benchmark_Logger_ShortString_WithFieldAndArgs(b *testing.B) {
benchmarkLoggerWithField(b, testStringShort+"%s", "I'm real")
}
func Benchmark_Logger_VeryLongString(b *testing.B) {
benchmarkLogger(b, testStringLong)
}
func Benchmark_Logger_VeryLongString_WithArgs(b *testing.B) {
benchmarkLogger(b, testStringLong+"%s", "I'm real")
}
func Benchmark_Logger_VeryLongString_WithFields(b *testing.B) {
benchmarkLoggerWithField(b, testStringLong)
}
func Benchmark_Logger_VeryLongString_WithFieldsAndArgs(b *testing.B) {
benchmarkLoggerWithField(b, testStringLong+"%s", "I'm real")
}
func benchmarkLogger(b *testing.B, format string, args ...interface{}) {
l := jlo.NewLogger(ioutil.Discard)
for i := 0; i < b.N; i++ {
l.Infof(format, args...)
}
}
func benchmarkLoggerWithField(b *testing.B, format string, args ...interface{}) {
l := jlo.NewLogger(ioutil.Discard)
for i := 0; i < b.N; i++ {
l.WithField("I'm", "real").Infof(format, args...)
}
}