-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
357 lines (306 loc) Β· 14.6 KB
/
example_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
package zl_test
import (
"encoding/json"
"fmt"
"github.com/nkmr-jp/zl"
"go.uber.org/zap"
"log"
"os"
"syscall"
"time"
)
var (
version string // version git revision or tag. set from go cli.
srcRootDir string // srcRootDir set from cli.
)
func setupForExampleTest() {
if err := os.RemoveAll("./log"); err != nil {
log.Fatal(err)
}
zl.ResetGlobalLoggerSettings() // removes logger and resets settings.
zl.SetIsTest()
}
func Example() {
setupForExampleTest()
// Set options
fileName := "./log/example.jsonl"
zl.SetLevel(zl.DebugLevel)
// zl.SetOutput(zl.PrettyOutput)
zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)
zl.SetRotateFileName(fileName)
// Initialize
zl.Init()
defer zl.Sync() // flush log buffer
// Write logs
zl.Info("USER_INFO", zap.String("user_name", "Alice"), zap.Int("user_age", 20)) // can use zap fields.
console := "display to console when output type is pretty"
zl.Info("DISPLAY_TO_CONSOLE", zl.Console(console))
zl.Info("DISPLAY_TO_CONSOLE", zl.Consolep(&console))
zl.Info("DISPLAY_TO_CONSOLE", zl.Consolef("message: %s", console))
// write error to error field.
_, err := os.ReadFile("test")
zl.Info("READ_FILE_ERROR", zap.Error(err))
zl.InfoErr("READ_FILE_ERROR", err) // same to above.
zl.Debug("READ_FILE_ERROR", zap.Error(err))
zl.DebugErr("READ_FILE_ERROR", err) // same to above.
zl.Warn("READ_FILE_ERROR", zap.Error(err))
zl.WarnErr("READ_FILE_ERROR", err) // same to above.
zl.Error("READ_FILE_ERROR", zap.Error(err))
zl.ErrorErr("READ_FILE_ERROR", err) // same to above.
zl.Err("READ_FILE_ERROR", err) // same to above.
zl.ErrRet("READ_FILE_ERROR", err) // write error to log and return same error.
zl.Fatal("READ_FILE_ERROR", zap.Error(err))
zl.FatalErr("READ_FILE_ERROR", err) // same to above.
fmt.Println("\nlog file output:")
bytes, _ := os.ReadFile(fileName)
fmt.Println(string(bytes))
// Output to stderr with colored:
// zl.go:82: DEBUG INIT_LOGGER:Severity: DEBUG, Output: Pretty, File: ./log/example.jsonl
// example_test.go:40: INFO USER_INFO
// example_test.go:43: INFO DISPLAY_TO_CONSOLE:display to console when output type is pretty
// example_test.go:44: INFO DISPLAY_TO_CONSOLE:display to console when output type is pretty
// example_test.go:45: INFO DISPLAY_TO_CONSOLE:message: display to console when output type is pretty
// example_test.go:49: INFO READ_FILE_ERROR
// example_test.go:50: INFO READ_FILE_ERROR:open test: no such file or directory
// example_test.go:51: DEBUG READ_FILE_ERROR
// example_test.go:52: DEBUG READ_FILE_ERROR:open test: no such file or directory
// example_test.go:53: WARN READ_FILE_ERROR
// example_test.go:54: WARN READ_FILE_ERROR:open test: no such file or directory
// example_test.go:55: ERROR READ_FILE_ERROR
// example_test.go:56: ERROR READ_FILE_ERROR:open test: no such file or directory
// example_test.go:57: ERROR READ_FILE_ERROR:open test: no such file or directory
// example_test.go:58: ERROR READ_FILE_ERROR:open test: no such file or directory
// example_test.go:59: FATAL READ_FILE_ERROR
// example_test.go:60: FATAL READ_FILE_ERROR:open test: no such file or directory
// Output:
// os.Exit(1) called.
// os.Exit(1) called.
//
// log file output:
// {"severity":"DEBUG","function":"github.com/nkmr-jp/zl.Init.func1","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Pretty, File: ./log/example.jsonl"}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"USER_INFO","user_name":"Alice","user_age":20}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"DISPLAY_TO_CONSOLE","console":"display to console when output type is pretty"}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"DISPLAY_TO_CONSOLE","console":"display to console when output type is pretty"}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"DISPLAY_TO_CONSOLE","console":"message: display to console when output type is pretty"}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"INFO","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"DEBUG","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"DEBUG","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"WARN","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"WARN","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"ERROR","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"ERROR","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"ERROR","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"ERROR","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"FATAL","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
// {"severity":"FATAL","function":"github.com/nkmr-jp/zl_test.Example","message":"READ_FILE_ERROR","error":"open test: no such file or directory"}
}
func ExampleSetVersion() {
setupForExampleTest()
urlFormat := "https://github.com/nkmr-jp/zl/blob/%s"
// Actually, it is recommended to pass the value from the command line of go.
// ex. `go run -ldflags "-X main.version=v1.0.0 -X main.srcRootDir=$PWD" main.go`.
version = "v1.0.0"
srcRootDir, _ = os.Getwd()
// Set Options
zl.SetLevel(zl.DebugLevel)
zl.SetVersion(version)
fileName := fmt.Sprintf("./log/example-set-version_%s.jsonl", zl.GetVersion())
zl.SetRotateFileName(fileName)
zl.SetRepositoryCallerEncoder(urlFormat, version, srcRootDir)
zl.SetOmitKeys(zl.TimeKey, zl.FunctionKey, zl.HostnameKey, zl.PIDKey)
zl.SetOutput(zl.ConsoleAndFileOutput)
// Initialize
zl.Init()
defer zl.Sync() // flush log buffer
// Write logs
zl.Info("INFO_MESSAGE", zap.String("detail", "detail info xxxxxxxxxxxxxxxxx"))
zl.Warn("WARN_MESSAGE", zap.String("detail", "detail info xxxxxxxxxxxxxxxxx"))
bytes, _ := os.ReadFile(fileName)
fmt.Println(string(bytes))
// Output:
// {"severity":"DEBUG","caller":"zl/zl.go:86","message":"INIT_LOGGER","version":"v1.0.0","console":"Severity: DEBUG, Output: ConsoleAndFile, File: ./log/example-set-version_v1.0.0.jsonl"}
// {"severity":"INFO","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L135","message":"INFO_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}
// {"severity":"WARN","caller":"https://github.com/nkmr-jp/zl/blob/v1.0.0/example_test.go#L136","message":"WARN_MESSAGE","version":"v1.0.0","detail":"detail info xxxxxxxxxxxxxxxxx"}
}
func ExampleNew() {
setupForExampleTest()
// Set options
traceIDField := "trace"
fileName := "./log/example-new.jsonl"
zl.SetConsoleFields(traceIDField)
zl.SetLevel(zl.DebugLevel)
zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.FunctionKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)
zl.SetOutput(zl.PrettyOutput)
zl.SetRotateFileName(fileName)
traceID := "c7mg6hnr2g4l6vvuao50" // xid.New().String()
// Initialize
zl.Init()
defer zl.Sync() // flush log buffer
// New
// e.g. Use this when you want to add a common value in the scope of a context, such as an API request.
log := zl.New(
zap.Int("user_id", 1),
zap.String(traceIDField, traceID),
)
emptyName := log.Named("")
named1 := log.Named("named1")
named2 := log.Named("named2")
named3 := named1.Named("named3") // named1.named3
// Write logs
err := fmt.Errorf("error")
zl.Info("GLOBAL_INFO")
log.Info("CONTEXT_SCOPE_INFO", zl.Consolef("some message to console: %s", "test"))
emptyName.Err("CONTEXT_SCOPE_ERROR", fmt.Errorf("context scope error message"))
named1.Info("CONTEXT_SCOPE_INFO2", zl.Consolef("some message to console: %s", "test"))
named2.Debug("TEST")
named3.Warn("TEST")
log.Error("TEST")
named1.Err("TEST", err)
named2.ErrorErr("TEST", err)
named3.ErrRet("TEST", err) // write error to log and return same error.
log.InfoErr("TEST", err)
named1.DebugErr("TEST", err)
named2.WarnErr("TEST", err)
named3.Fatal("TEST")
log.FatalErr("TEST", err)
fmt.Println("\nlog file output:")
bytes, _ := os.ReadFile(fileName)
fmt.Println(string(bytes))
// Output to stderr with colored:
// zl.go:82: DEBUG INIT_LOGGER Severity: DEBUG, Output: Pretty, File: ./log/example-new.jsonl
// example_test.go:176: INFO GLOBAL_INFO
// example_test.go:177: INFO CONTEXT_SCOPE_INFO some message to console: test c7mg6hnr2g4l6vvuao50
// example_test.go:178: ERROR CONTEXT_SCOPE_ERROR context scope error message c7mg6hnr2g4l6vvuao50
// named1 | example_test.go:179: INFO CONTEXT_SCOPE_INFO2 some message to console: test c7mg6hnr2g4l6vvuao50
// named2 | example_test.go:180: DEBUG TEST c7mg6hnr2g4l6vvuao50
// named1.named3 | example_test.go:181: WARN TEST c7mg6hnr2g4l6vvuao50
// example_test.go:182: ERROR TEST c7mg6hnr2g4l6vvuao50
// named1 | example_test.go:183: ERROR TEST error c7mg6hnr2g4l6vvuao50
// named2 | example_test.go:184: ERROR TEST error c7mg6hnr2g4l6vvuao50
// named1.named3 | example_test.go:185: ERROR TEST error c7mg6hnr2g4l6vvuao50
// example_test.go:186: INFO TEST error c7mg6hnr2g4l6vvuao50
// named1 | example_test.go:187: DEBUG TEST error c7mg6hnr2g4l6vvuao50
// named2 | example_test.go:188: WARN TEST error c7mg6hnr2g4l6vvuao50
// named1.named3 | example_test.go:189: FATAL TEST c7mg6hnr2g4l6vvuao50
// example_test.go:190: FATAL TEST error c7mg6hnr2g4l6vvuao50
// Output:
// os.Exit(1) called.
// os.Exit(1) called.
//
// log file output:
// {"severity":"DEBUG","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Pretty, File: ./log/example-new.jsonl"}
// {"severity":"INFO","message":"GLOBAL_INFO"}
// {"severity":"INFO","message":"CONTEXT_SCOPE_INFO","console":"some message to console: test","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"ERROR","message":"CONTEXT_SCOPE_ERROR","error":"context scope error message","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"INFO","logger":"named1","message":"CONTEXT_SCOPE_INFO2","console":"some message to console: test","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"DEBUG","logger":"named2","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"WARN","logger":"named1.named3","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"ERROR","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"ERROR","logger":"named1","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"ERROR","logger":"named2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"ERROR","logger":"named1.named3","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"INFO","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"DEBUG","logger":"named1","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"WARN","logger":"named2","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"FATAL","logger":"named1.named3","message":"TEST","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
// {"severity":"FATAL","message":"TEST","error":"error","user_id":1,"trace":"c7mg6hnr2g4l6vvuao50"}
}
func ExampleSetLevelByString() {
setupForExampleTest()
zl.SetLevelByString("DEBUG")
zl.SetOutputByString("Console")
zl.SetStdout()
zl.SetOmitKeys(zl.TimeKey, zl.CallerKey, zl.FunctionKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey)
zl.Init()
zl.Debug("DEBUG_MESSAGE")
zl.Info("INFO_MESSAGE")
// Output:
// {"severity":"DEBUG","message":"INIT_LOGGER","console":"Severity: DEBUG, Output: Console"}
// {"severity":"DEBUG","message":"DEBUG_MESSAGE"}
// {"severity":"INFO","message":"INFO_MESSAGE"}
}
func ExampleSetOmitKeys() {
setupForExampleTest()
zl.SetOutputByString("Console")
zl.SetStdout()
zl.SetOmitKeys(
zl.MessageKey, zl.LevelKey, zl.LoggerKey, zl.TimeKey,
zl.CallerKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey,
)
zl.Init()
zl.Info("INFO_MESSAGE")
// Output:
// {"function":"github.com/nkmr-jp/zl_test.ExampleSetOmitKeys"}
}
func ExampleSetFieldKey() {
setupForExampleTest()
zl.SetOutputByString("Console")
zl.SetStdout()
zl.SetOmitKeys(
zl.LevelKey, zl.LoggerKey, zl.TimeKey,
zl.CallerKey, zl.VersionKey, zl.HostnameKey, zl.StacktraceKey, zl.PIDKey,
)
zl.SetFieldKey(zl.MessageKey, "msg")
zl.SetFieldKey(zl.FunctionKey, "fn")
zl.Init()
zl.Info("INFO_MESSAGE")
// Output:
// {"fn":"github.com/nkmr-jp/zl_test.ExampleSetFieldKey","msg":"INFO_MESSAGE"}
}
func ExampleError() {
setupForExampleTest()
zl.SetOmitKeys(zl.TimeKey, zl.VersionKey, zl.HostnameKey)
// Initialize
zl.Init()
defer zl.Sync() // flush log buffer
_, err := os.ReadFile("test")
zl.Err("READ_FILE_ERROR", err)
zl.Info("INFO")
zl.InfoErr("INFO_ERR", fmt.Errorf("error"))
v := ""
err = json.Unmarshal([]byte("test"), &v)
zl.Err("JSON_UNMARSHAL_ERROR", err)
for i := 0; i < 3; i++ {
err = fmt.Errorf("if the same error occurs multiple times in the same location, the error report will show them all together")
zl.Err("ERRORS_IN_LOOPS", err)
}
// Output:
}
func ExampleDump() {
setupForExampleTest()
zl.SetLevel(zl.DebugLevel)
zl.SetRotateFileName("./log/example-Dump.jsonl")
zl.Init()
defer zl.Sync() // flush log buffer
zl.Dump("test")
// Output:
}
func ExampleSyncWhenStop() {
// syscall.SIGINT
setupForExampleTest()
zl.SetLevel(zl.DebugLevel)
zl.SetRotateFileName("./log/example-SyncWhenStop.jsonl")
zl.Init()
zl.SyncWhenStop()
go func() {
time.Sleep(time.Millisecond * 50)
syscall.Kill(os.Getpid(), syscall.SIGINT)
}()
time.Sleep(time.Millisecond * 100)
// syscall.SIGTERM
fmt.Println()
setupForExampleTest()
zl.SetLevel(zl.DebugLevel)
zl.SetRotateFileName("./log/example-SyncWhenStop.jsonl")
zl.Init()
zl.SyncWhenStop()
go func() {
time.Sleep(time.Millisecond * 50)
syscall.Kill(os.Getpid(), syscall.SIGTERM)
}()
time.Sleep(time.Millisecond * 100)
// Output:
// os.Exit(130) called.
// os.Exit(143) called.
}