/
log4go_test.go
101 lines (76 loc) · 2.42 KB
/
log4go_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
package log4go
import (
"context"
"testing"
)
func TestLogger(t *testing.T) {
ctx := context.TODO()
ctx = context.WithValue(ctx, ContextFieldsKey, []Field{
String("s0", "context field"),
})
// dlog := NewFileLogger(WithLevel("debug"), WithCaller(false), WithStack(false))
clog := NewConsoleLogger(WithLevel("debug"),
WithStack(false),
WithExtendFields(String("s1", "ext field1"), Int64("i2", 3)))
// glog := NewGroupLogger(dlog, clog)
SetDefaultLogger(clog)
// defer Sync(ctx)
Info(ctx, "test1")
Debug(ctx, "debug test", Int("t2", 2))
Error(ctx, "error test", String("t", "www.baidu.com"))
Warn(ctx, "warn test", String("w", "bilibili.com"))
// Panic(ctx, "panic")
}
func TestMultiLogger(t *testing.T) {
ctx := context.TODO()
ctx = context.WithValue(ctx, ContextFieldsKey, []Field{
String("s0", "context field"),
})
// dlog := NewFileLogger(WithLevel("debug"), WithCaller(false), WithStack(false))
clog := NewConsoleLogger(WithLevel("debug"),
WithStack(false),
WithExtendFields(String("s1", "ext field1"), Int64("i2", 3)))
// glog := NewGroupLogger(dlog, clog)
// SetDefaultLogger(clog)
SetLogger("console", clog)
// defer Sync(ctx)
GetLogger("console").Info(ctx, "test1")
GetLogger("console").Debug(ctx, "debug test", Int("t2", 2))
GetLogger("console").Error(ctx, "error test", String("t", "www.baidu.com"))
GetLogger("console").Warn(ctx, "warn test", String("w", "bilibili.com"))
Info(ctx, "nil")
}
func BenchmarkConsoleLogger(b *testing.B) {
ctx := context.TODO()
ctx = context.WithValue(ctx, ContextFieldsKey, []Field{
String("s0", "context field"),
})
dlog := NewFileLogger(
WithFileName("/tmp/benchmark.log"),
WithLevel("debug"),
WithCaller(true),
WithStack(true), WithExtendFields(String("s1", "ext field1"), Int64("i2", 3)))
// clog := NewConsoleLogger(WithLevel("debug"), WithStack(false))
// glog := NewGroupLogger(dlog, clog)
SetDefaultLogger(dlog)
// defer Sync(ctx)
for i := 0; i < b.N; i++ {
// Info(ctx, "test1")
Debug(ctx, "debug test",
Int("t2", 2),
String("t", "www.baidu.com"),
Int("t3", 2),
String("t4", "www.baidu.com"),
Int("t5", 2),
String("t6", "www.baidu.com"),
Int("t7", 2),
String("t8", "www.baidu.com"),
Int("t9", 2),
String("t10", "www.baidu.com"),
Int("t11", 2),
String("t12", "www.baidu.com"),
)
// Error(ctx, "error test", String("t", "www.baidu.com"))
// Warn(ctx, "warn test", String("w", "bilibili.com"))
}
}