-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_test.go
160 lines (138 loc) · 4.49 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
package wzap_test
import (
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"github.com/sevennt/wzap"
)
// go test github.com/sevennt/wzap -v -run=TestFileWrite$
func TestFileWrite(t *testing.T) {
temp, err := ioutil.TempFile("/tmp", "async")
require.NoError(t, err, "Failed to create temp file.")
defer os.Remove(temp.Name())
logger := wzap.New(
wzap.WithPath(temp.Name()), // 日志写入文件为/tmp/async.log
wzap.WithLevel(wzap.Error), // 日志写入级别为Error,此时只有级别大于等于Error级别日志才会被写入
)
assert.NoError(t, err)
// 写入Info级别日志(由于本级别小小于Error级别,此日志不会被写入)
logger.Info("some info message",
"scenario", "classroom",
"Hanmeimei", "How are you?",
"Lilei", "I'm fine, thank you.",
)
// 写入Error级别日志(此级别大于等于Error级别,会被写入)
logger.Error("some error message",
"scenario", "classroom",
"Hanmeimei", "How are you?",
"Lilei", "I'm fine, thank you.",
)
countMsg := "sampling"
expectCount := 200
for i := 0; i < expectCount; i++ {
logger.Error(countMsg)
}
byteContents, err := ioutil.ReadAll(temp)
require.NoError(t, err, "Couldn't read log contents from temp file.")
logs := string(byteContents)
assert.Contains(t, logs, `"scenario":"classroom","Hanmeimei":"How are you?","Lilei":"I'm fine, thank you."`, "Unexpected log output.")
assert.Equal(t, expectCount, strings.Count(logs, countMsg))
}
func BenchmarkStructedLogError(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
logger := wzap.New(
wzap.WithPath(temp.Name()), // 日志写入文件为/tmp/async.log
wzap.WithLevel(wzap.Error), // 日志写入级别为Error,此时只有级别大于等于Error级别日志才会被写入
)
for i := 0; i < b.N; i++ {
logger.Error("random int bench",
"random", rand.Intn(100),
)
}
}
func BenchmarkStructedLogInfo(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
logger := wzap.New(
wzap.WithPath(temp.Name()), // 日志写入文件为/tmp/async.log。
wzap.WithLevel(wzap.Info), // 日志写入级别为Error,此时只有级别大于等于Error级别日志才会被写入。
)
for i := 0; i < b.N; i++ {
logger.Info("random int bench",
"random", rand.Intn(100),
)
}
}
func BenchmarkConsoleLogInfo(b *testing.B) {
logger := wzap.New(
wzap.WithLevel(wzap.Debug),
wzap.WithColorful(true),
wzap.WithAsync(false),
wzap.WithPrefix("APP]>"),
)
for i := 0; i < b.N; i++ {
logger.Info("random int bench",
"random", rand.Intn(100),
)
}
}
func BenchmarkPrintfLogError(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
logger := wzap.New(
wzap.WithPath(temp.Name()), // 日志写入文件为/tmp/async.log
wzap.WithLevel(wzap.Error), // 日志写入级别为Error,此时只有级别大于等于Error级别日志才会被写入
)
for i := 0; i < b.N; i++ {
logger.Errorf("random_int:%d", rand.Intn(100))
}
}
func BenchmarkPrintfLogInfo(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
logger := wzap.New(
wzap.WithPath(temp.Name()), // 日志写入文件为/tmp/async.log
wzap.WithLevel(wzap.Info), // 日志写入级别为Error,此时只有级别大于等于Error级别日志才会被写入
)
for i := 0; i < b.N; i++ {
logger.Infof("random_int:%d", rand.Intn(100))
}
}
func BenchmarkZapLogError(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
cfg := zap.NewProductionConfig()
cfg.OutputPaths = []string{temp.Name()}
myLog, _ := cfg.Build()
for i := 0; i < b.N; i++ {
myLog.Sugar().Errorw(
"random int bench",
"random", rand.Intn(100),
)
}
}
func BenchmarkZapLogInfo(b *testing.B) {
temp, err := ioutil.TempFile("/tmp", "async")
defer os.Remove(temp.Name())
require.NoError(b, err, "Failed to create temp file.")
cfg := zap.NewProductionConfig()
cfg.OutputPaths = []string{temp.Name()}
myLog, _ := cfg.Build()
for i := 0; i < b.N; i++ {
myLog.Sugar().Infow(
"random int bench",
"random", rand.Intn(100),
)
}
}