This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlog_test.go
296 lines (249 loc) · 9.03 KB
/
xlog_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
package xlog_test
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/goinsane/xlog"
)
var (
testTime, _ = time.ParseInLocation("2006-01-02T15:04:05", "2010-11-12T13:14:15", time.Local)
)
// resetForTest resets xlog to run new test.
func resetForTest() {
xlog.Reset()
xlog.SetFlags(xlog.FlagDefault & ^xlog.FlagDate & ^xlog.FlagTime & ^xlog.FlagStackTrace)
xlog.SetOutputWriter(os.Stdout)
}
func Example() {
// reset xlog for previous changes if it is running in go test.
xlog.Reset()
// change writer of default output to stdout from stderr.
xlog.SetOutputWriter(os.Stdout)
// log by Severity.
// default severity is SeverityInfo.
// default verbose is 0.
xlog.Debug("this is debug log. but it won't be shown.")
xlog.Info("this is info log.")
xlog.Warning("this is warning log.")
xlog.V(1).Error("this is error log, verbosity 1. but it won't be shown.")
// SetSeverity()
xlog.SetSeverity(xlog.SeverityDebug)
xlog.Debug("this is debug log. it will now be shown.")
// SetVerbose() and V()
xlog.SetVerbose(1)
xlog.V(1).Error("this is error log, verbosity 1. it will now be shown.")
xlog.V(2).Warning("this is warning log, verbosity 2. it won't be shown.")
// SetFlags()
// default flags is FlagDefault.
xlog.SetFlags(xlog.FlagDefault | xlog.FlagShortFile)
xlog.Info("this is info log. you can see file name and line in this log.")
// log using Print.
// default print severity is SeverityInfo.
xlog.Print("this log will be shown as info log.")
// SetPrintSeverity()
xlog.SetPrintSeverity(xlog.SeverityWarning)
xlog.Print("this log will now be shown as warning log.")
// SetStackTraceSeverity()
// default stack trace severity is none.
xlog.SetStackTraceSeverity(xlog.SeverityWarning)
xlog.Warning("this is warning log. you can see stack trace end of this log.")
xlog.Error("this is error log. you can still see stack trace end of this log.")
xlog.Info("this is info log. stack trace won't be shown end of this log.")
// WithPrefix()
xlog.WithPrefix("prefix1").Warning("this is warning log with prefix 'prefix1'.")
xlog.WithPrefix("prefix1").WithPrefix("prefix2").Error("this is error log with both of prefixes 'prefix1' and 'prefix2'.")
// WithTime()
xlog.WithTime(testTime).Info("this is info log with custom time.")
// WithFieldKeyVals()
xlog.WithFieldKeyVals("key1", "val1", "key2", "val2", "key3", "val3", "key1", "val1-2", "key2", "val2-2").Info("this is info log with several fields.")
}
func Example_test1() {
// reset xlog for previous changes if it is running in go test.
xlog.Reset()
// just show severity.
xlog.SetFlags(xlog.FlagSeverity)
// change writer of default output to stdout from stderr.
xlog.SetOutputWriter(os.Stdout)
xlog.Debug("this is debug log, verbosity 0. it will not be shown.")
xlog.Info("this is info log, verbosity 0.")
xlog.Warning("this is warning log, verbosity 0.")
xlog.Error("this is error log, verbosity 0.")
xlog.Print("this is info log, verbosity 0 caused by Print().")
xlog.V(1).Info("this is info log, verbosity 1. it will not be shown.")
xlog.SetSeverity(xlog.SeverityDebug)
xlog.Debug("this is debug log, verbosity 0.")
xlog.SetVerbose(1)
xlog.V(0).Info("this is info log, verbosity 0.")
xlog.V(1).Info("this is info log, verbosity 1.")
xlog.V(2).Info("this is info log, verbosity 2. it will not be shown.")
xlog.SetPrintSeverity(xlog.SeverityWarning)
xlog.Print("this is warning log, verbosity 0 caused by Print().")
xlog.Warning("this is warning log, verbosity 0.\nwithout padding.")
xlog.SetFlags(xlog.FlagSeverity | xlog.FlagPadding)
xlog.Warning("this is warning log, verbosity 0.\nwith padding.")
xlog.SetFlags(xlog.FlagDefault)
tm, _ := time.ParseInLocation("2006-01-02T15:04:05", "2019-11-13T21:56:24", time.Local)
xlog.WithTime(tm).Info("this is info log, verbosity 0.")
// Output:
// INFO - this is info log, verbosity 0.
// WARNING - this is warning log, verbosity 0.
// ERROR - this is error log, verbosity 0.
// INFO - this is info log, verbosity 0 caused by Print().
// DEBUG - this is debug log, verbosity 0.
// INFO - this is info log, verbosity 0.
// INFO - this is info log, verbosity 1.
// WARNING - this is warning log, verbosity 0 caused by Print().
// WARNING - this is warning log, verbosity 0.
// without padding.
// WARNING - this is warning log, verbosity 0.
// with padding.
// 2019/11/13 21:56:24 INFO - this is info log, verbosity 0.
}
func ExampleSetSeverity() {
resetForTest()
xlog.SetSeverity(xlog.SeverityDebug)
xlog.Debug("this is debug log, verbosity 0.")
xlog.Info("this is info log, verbosity 0.")
xlog.Warning("this is warning log, verbosity 0.")
// Output:
// DEBUG - this is debug log, verbosity 0.
// INFO - this is info log, verbosity 0.
// WARNING - this is warning log, verbosity 0.
}
func ExampleSetVerbose() {
resetForTest()
xlog.SetVerbose(2)
xlog.V(0).Debug("this is debug log, verbosity 0. it won't be shown.")
xlog.V(1).Info("this is info log, verbosity 1.")
xlog.V(2).Warning("this is warning log, verbosity 2.")
xlog.V(3).Error("this is error log, verbosity 3. it won't be shown.")
// Output:
// INFO - this is info log, verbosity 1.
// WARNING - this is warning log, verbosity 2.
}
func ExampleSetFlags() {
resetForTest()
xlog.SetFlags(0)
xlog.Info("this is info log, verbosity 0.")
// Output:
// this is info log, verbosity 0.
}
func ExampleWithTime() {
resetForTest()
xlog.SetFlags(xlog.FlagDefault)
xlog.WithTime(testTime).Info("this is info log, verbosity 0.")
// Output:
// 2010/11/12 13:14:15 INFO - this is info log, verbosity 0.
}
func ExampleLogger() {
logger := xlog.New(xlog.NewTextOutput(os.Stdout), xlog.SeverityInfo, 2)
logger.SetFlags(xlog.FlagSeverity)
logger.Info("this is info log, verbosity 0.")
logger.V(0).Info("this is info log, verbosity 0.")
logger.V(1).Warning("this is warning log, verbosity 1.")
logger.V(2).Error("this is error log, verbosity 2.")
logger.V(3).Error("this is error log, verbosity 3. it won't be shown.")
logger.Debug("this is debug log, verbosity 0. it won't be shown.")
// Output:
// INFO - this is info log, verbosity 0.
// INFO - this is info log, verbosity 0.
// WARNING - this is warning log, verbosity 1.
// ERROR - this is error log, verbosity 2.
}
func BenchmarkLogger_Info(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_Infof(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Infof("%s", "benchmark")
}
}
func BenchmarkLogger_Infoln(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Infoln("benchmark")
}
}
func BenchmarkLogger_Info_withStackTrace(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
logger.SetStackTraceSeverity(xlog.SeverityInfo)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_Info_withFlagLongFunc(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
logger.SetFlags(xlog.FlagDefault | xlog.FlagLongFunc)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_Info_withFlagShortFunc(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
logger.SetFlags(xlog.FlagDefault | xlog.FlagShortFunc)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_Info_withFlagLongFile(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
logger.SetFlags(xlog.FlagDefault | xlog.FlagLongFile)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_Info_withFlagShortFile(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
logger.SetFlags(xlog.FlagDefault | xlog.FlagShortFile)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("benchmark")
}
}
func BenchmarkLogger_V(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 5)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.V(1)
}
}
func BenchmarkLogger_WithPrefix(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.WithPrefix("prefix")
}
}
func BenchmarkLogger_WithPrefixf(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.WithPrefixf("%s", "prefix")
}
}
func BenchmarkLogger_WithTime(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.WithTime(testTime)
}
}
func BenchmarkLogger_WithFieldKeyVals(b *testing.B) {
logger := xlog.New(xlog.NewTextOutput(ioutil.Discard), xlog.SeverityInfo, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.WithFieldKeyVals("key1", "value1")
}
}