-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_async_test.go
557 lines (475 loc) · 14.6 KB
/
logger_async_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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xlog/blob/main/LICENSE.
package xlog_test
import (
"bytes"
"encoding/json"
"errors"
"io"
"os"
"strings"
"sync"
"testing"
"github.com/actforgood/xlog"
)
func ExampleAsyncLogger() {
// In this example we create a (async)logger that writes
// logs to standard output.
opts := xlog.NewCommonOpts()
opts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
opts.AdditionalKeyValues = []any{
"appName", "demo",
"env", "dev",
}
opts.Time = func() any { // mock time for output check
return "2022-03-16T16:01:20Z"
}
opts.Source = xlog.SourceProvider(4, 1) // keep only filename for output check
logger := xlog.NewAsyncLogger(
os.Stdout,
xlog.AsyncLoggerWithOptions(opts),
xlog.AsyncLoggerWithWorkersNo(2), // since workers no > 1, we expect output to be unordered.
)
defer logger.Close()
logger.Log(xlog.MessageKey, "Hello World", "year", 2022)
logger.Debug(xlog.MessageKey, "Hello World", "year", 2022)
logger.Info(xlog.MessageKey, "Hello World", "year", 2022)
logger.Warn(xlog.MessageKey, "Hello World", "year", 2022)
logger.Error(xlog.MessageKey, "Could not read file", xlog.ErrorKey, io.ErrUnexpectedEOF, "file", "/some/file")
logger.Critical(xlog.MessageKey, "DB connection is down")
// Unordered output:
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","msg":"Hello World","src":"/logger_async_test.go:43","year":2022}
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","lvl":"DEBUG","msg":"Hello World","src":"/logger_async_test.go:44","year":2022}
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","lvl":"INFO","msg":"Hello World","src":"/logger_async_test.go:45","year":2022}
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","lvl":"WARN","msg":"Hello World","src":"/logger_async_test.go:46","year":2022}
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","err":"unexpected EOF","file":"/some/file","lvl":"ERROR","msg":"Could not read file","src":"/logger_async_test.go:47"}
// {"appName":"demo","date":"2022-03-16T16:01:20Z","env":"dev","lvl":"CRITICAL","msg":"DB connection is down","src":"/logger_async_test.go:48"}
}
func TestAsyncLogger_Log(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelNone
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func TestAsyncLogger_Debug(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelDebug
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func TestAsyncLogger_Info(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelInfo
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func TestAsyncLogger_Warn(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelWarning
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func TestAsyncLogger_Error(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelError
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func TestAsyncLogger_Critical(t *testing.T) {
t.Parallel()
testLvl := xlog.LevelCritical
t.Run("success", testAsyncLoggerLogSuccessful(testLvl))
t.Run("ignored", testAsyncLoggerLogIgnored(testLvl))
t.Run("format write err", testAsyncLoggerLogFormatErr(testLvl))
}
func testAsyncLoggerLogSuccessful(testLvl xlog.Level) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
writer = io.Discard
formatter = new(MockFormatter)
errHandler = new(MockErrorHandler)
commOpts = xlog.NewCommonOpts()
subject xlog.Logger = xlog.NewAsyncLogger(
writer,
xlog.AsyncLoggerWithFormatter(formatter.Format),
xlog.AsyncLoggerWithOptions(commOpts),
)
)
commOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
commOpts.MaxLevel = xlog.FixedLevelProvider(xlog.LevelCritical)
commOpts.AdditionalKeyValues = getAdditionalKeyValues()
commOpts.ErrHandler = errHandler.Handle
commOpts.SourceKey = ""
commOpts.Time = staticTimeProvider
formatter.SetFormatCallback(func(w io.Writer, kv []any) error {
assertEqual(t, getExpectedKeyValues(testLvl, commOpts.LevelLabels), kv)
assertEqual(t, writer, w)
return nil
})
// act
callMethodByLevel(subject, testLvl)
_ = subject.Close()
// assert
assertEqual(t, 1, formatter.FormatCallsCount())
assertEqual(t, 0, errHandler.HandleCallsCount())
}
}
func testAsyncLoggerLogIgnored(testLvl xlog.Level) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
writer = io.Discard
formatter = new(MockFormatter)
errHandler = new(MockErrorHandler)
commOpts = xlog.NewCommonOpts()
subject = xlog.NewAsyncLogger(
writer,
xlog.AsyncLoggerWithFormatter(formatter.Format),
xlog.AsyncLoggerWithOptions(commOpts),
)
)
if testLvl != xlog.LevelError {
commOpts.MinLevel = xlog.FixedLevelProvider(testLvl + 1)
commOpts.MaxLevel = xlog.FixedLevelProvider(xlog.LevelCritical)
} else {
commOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
commOpts.MaxLevel = xlog.FixedLevelProvider(testLvl - 1)
}
commOpts.ErrHandler = errHandler.Handle
// act
callMethodByLevel(subject, testLvl)
_ = subject.Close()
// assert
assertEqual(t, 0, formatter.FormatCallsCount())
assertEqual(t, 0, errHandler.HandleCallsCount())
}
}
func testAsyncLoggerLogFormatErr(testLvl xlog.Level) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
// arrange
var (
writer = io.Discard
formatter = new(MockFormatter)
errHandler = new(MockErrorHandler)
commOpts = xlog.NewCommonOpts()
subject = xlog.NewAsyncLogger(
writer,
xlog.AsyncLoggerWithFormatter(formatter.Format),
xlog.AsyncLoggerWithOptions(commOpts),
)
)
commOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
commOpts.MaxLevel = xlog.FixedLevelProvider(xlog.LevelCritical)
commOpts.AdditionalKeyValues = getAdditionalKeyValues()
commOpts.ErrHandler = errHandler.Handle
commOpts.SourceKey = ""
commOpts.Time = staticTimeProvider
formatter.SetFormatCallback(FormatCallbackErr)
errHandler.SetHandleCallback(func(err error, keyVals []any) {
assertTrue(t, errors.Is(err, ErrFormat))
assertEqual(t, getExpectedKeyValues(testLvl, commOpts.LevelLabels), keyVals)
})
// act
callMethodByLevel(subject, testLvl)
_ = subject.Close()
// assert
assertEqual(t, 1, formatter.FormatCallsCount())
assertEqual(t, 1, errHandler.HandleCallsCount())
}
}
func TestAsyncLogger_Close_withBufferedWriter(t *testing.T) {
t.Parallel()
// arrange
var (
writer bytes.Buffer
bufWriter = xlog.NewBufferedWriter(
&writer,
xlog.BufferedWriterWithSize(1024*1024),
xlog.BufferedWriterWithFlushInterval(0),
)
subject = xlog.NewAsyncLogger(
bufWriter,
xlog.AsyncLoggerWithChannelSize(1),
)
)
subject.Error("msg", "foo bar")
// act
_ = subject.Close() // will call Stop on bufWriter, and log gets flushed.
// assert
log, err := writer.ReadString('\n')
if err != nil {
t.Fatal(err)
}
assertTrue(t, strings.Contains(log, "foo bar"))
}
func TestAsyncLogger_concurrency(t *testing.T) {
t.Parallel()
// arrange
var (
errHandler = new(MockErrorHandler)
commOpts = xlog.NewCommonOpts()
goroutinesNo = 200
logsNo = 10
wg sync.WaitGroup
buf1, buf2 bytes.Buffer
writer1, writer2 io.Writer = &buf1, xlog.NewSyncWriter(&buf2)
tests = [...]struct {
name string
buf *bytes.Buffer
writer io.Writer
subject *xlog.AsyncLogger
}{
{
name: "one worker is safe for un-sync writer",
buf: &buf1,
subject: xlog.NewAsyncLogger(
writer1,
xlog.AsyncLoggerWithOptions(commOpts),
),
},
{
name: "more than one worker needs a sync writer",
buf: &buf2,
subject: xlog.NewAsyncLogger(
writer2,
xlog.AsyncLoggerWithOptions(commOpts),
xlog.AsyncLoggerWithWorkersNo(2),
),
},
}
)
commOpts.MinLevel = xlog.FixedLevelProvider(xlog.LevelNone)
commOpts.AdditionalKeyValues = getAdditionalKeyValues()
commOpts.ErrHandler = errHandler.Handle
commOpts.SourceKey = ""
commOpts.Time = staticTimeProvider
for _, testData := range tests {
test := testData // capture range variable
t.Run(test.name, func(t *testing.T) {
// act
for i := 0; i < goroutinesNo; i++ {
wg.Add(1)
go func(logger xlog.Logger, threadNo int) {
defer wg.Done()
for j := 0; j < logsNo; j++ {
keyValues := getInputKeyValues()
keyValues = append(keyValues, "threadNo", threadNo+1, "logNo", j+1)
logger.Log(keyValues...)
}
}(test.subject, i)
}
wg.Wait()
_ = test.subject.Close()
// assert
var linesCount, sum int
for {
line, err := test.buf.ReadBytes('\n')
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.Error(err.Error())
continue
}
linesCount++
var logData map[string]any
if err := json.Unmarshal(line, &logData); err != nil {
t.Error(err.Error())
continue
}
assertEqual(t, 6, len(logData))
assertEqual(t, staticTime, logData["date"])
assertEqual(t, "extraValue", logData["extraKey"])
assertEqual(t, "bar", logData["foo"])
assertEqual(t, float64(10), logData["no"])
sum += int(logData["threadNo"].(float64) * logData["logNo"].(float64))
}
assertEqual(t, 0, errHandler.HandleCallsCount())
assertEqual(t, goroutinesNo*logsNo, linesCount)
expectedSum := goroutinesNo * (goroutinesNo + 1) * logsNo * (logsNo + 1) / 4
assertEqual(t, expectedSum, sum)
})
}
}
func BenchmarkAsyncLogger_json_withDiscardWriter_with256ChanSize_with1Worker_sequential(b *testing.B) {
subject := makeAsyncLogger(io.Discard, 256, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withDiscardWriter_with256ChanSize_with1Worker_parallel(b *testing.B) {
subject := makeAsyncLogger(io.Discard, 256, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
subject.Error(kv...)
}
})
}
func BenchmarkAsyncLogger_json_withDiscardWriter_with256ChanSize_with4Workers(b *testing.B) {
subject := makeAsyncLogger(io.Discard, 256, 4)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withFileWriter_with256ChanSize_with1Worker(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(f, 256, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withBufferedFileWriter_with256ChanSize_with1Worker(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(xlog.NewBufferedWriter(f), 256, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withFileWriter_with1024ChanSize_with4Workers(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(f, 1024, 4)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withBufferedFileWriter_with1024ChanSize_with4Workers(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(xlog.NewBufferedWriter(f), 1024, 4)
defer subject.Close()
kv := getBenchmarkKeyVals()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
subject.Error(kv...)
}
}
func BenchmarkAsyncLogger_json_withDiscardWriter_with256ChanSize_with1Worker_withConcurrency10(b *testing.B) {
subject := makeAsyncLogger(io.Discard, 10, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
var wg sync.WaitGroup
goroutinesNo := 10
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
wg.Add(goroutinesNo)
for i := 0; i < goroutinesNo; i++ {
go func() {
defer wg.Done()
subject.Error(kv...)
}()
}
wg.Wait()
}
}
func BenchmarkAsyncLogger_json_withDiscardWriter_with1024ChanSize_with4Workers_withConcurrency100(b *testing.B) {
subject := makeAsyncLogger(io.Discard, 1024, 4)
defer subject.Close()
kv := getBenchmarkKeyVals()
var wg sync.WaitGroup
goroutinesNo := 100
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
wg.Add(goroutinesNo)
for i := 0; i < goroutinesNo; i++ {
go func() {
defer wg.Done()
subject.Error(kv...)
}()
}
wg.Wait()
}
}
func BenchmarkAsyncLogger_json_withFileWriter_with256ChanSize_with1Worker_withConcurrency10(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(f, 256, 1)
defer subject.Close()
kv := getBenchmarkKeyVals()
var wg sync.WaitGroup
goroutinesNo := 10
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
wg.Add(goroutinesNo)
for i := 0; i < goroutinesNo; i++ {
go func() {
defer wg.Done()
subject.Error(kv...)
}()
}
wg.Wait()
}
}
func BenchmarkAsyncLogger_json_withFileWriter_with1024ChanSize_with4Workers_withConcurrency100(b *testing.B) {
f := setUpFile(b.Name())
defer tearDownFile(f)
subject := makeAsyncLogger(f, 1024, 24)
defer subject.Close()
kv := getBenchmarkKeyVals()
var wg sync.WaitGroup
goroutinesNo := 100
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
wg.Add(goroutinesNo)
for i := 0; i < goroutinesNo; i++ {
go func() {
defer wg.Done()
subject.Error(kv...)
}()
}
wg.Wait()
}
}
// makeAsyncLogger creates a new AsyncLogger object.
func makeAsyncLogger(w io.Writer, chanSize uint16, workersNo uint16) *xlog.AsyncLogger {
commonOpts := xlog.NewCommonOpts()
commonOpts.Source = xlog.SourceProvider(4, 1)
return xlog.NewAsyncLogger(
w,
xlog.AsyncLoggerWithOptions(commonOpts),
xlog.AsyncLoggerWithChannelSize(chanSize),
xlog.AsyncLoggerWithWorkersNo(workersNo),
)
}