-
Notifications
You must be signed in to change notification settings - Fork 25
/
example_test.go
85 lines (69 loc) · 1.72 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
package slog_test
import (
"fmt"
"sync"
"time"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
)
func Example_quickStart() {
slog.Info("info log message")
slog.Warn("warning log message")
slog.Infof("info log %s", "message")
slog.Debugf("debug %s", "message")
}
func Example_configSlog() {
slog.Configure(func(logger *slog.SugaredLogger) {
f := logger.Formatter.(*slog.TextFormatter)
f.EnableColor = true
})
slog.Trace("this is a simple log message")
slog.Debug("this is a simple log message")
slog.Info("this is a simple log message")
slog.Notice("this is a simple log message")
slog.Warn("this is a simple log message")
slog.Error("this is a simple log message")
slog.Fatal("this is a simple log message")
}
func Example_useJSONFormat() {
// use JSON formatter
slog.SetFormatter(slog.NewJSONFormatter())
slog.Info("info log message")
slog.Warn("warning log message")
slog.WithData(slog.M{
"key0": 134,
"key1": "abc",
}).Infof("info log %s", "message")
r := slog.WithFields(slog.M{
"category": "service",
"IP": "127.0.0.1",
})
r.Infof("info %s", "message")
r.Debugf("debug %s", "message")
}
func ExampleNew() {
mylog := slog.New()
levels := slog.AllLevels
mylog.AddHandler(handler.MustFileHandler("app.log", handler.WithLogLevels(levels)))
mylog.Info("info log message")
mylog.Warn("warning log message")
mylog.Infof("info log %s", "message")
}
func ExampleFlushDaemon() {
wg := sync.WaitGroup{}
wg.Add(1)
go slog.FlushDaemon(func() {
fmt.Println("flush daemon stopped")
slog.MustClose()
wg.Done()
})
go func() {
// mock app running
time.Sleep(time.Second * 2)
// stop daemon
fmt.Println("stop flush daemon")
slog.StopDaemon()
}()
// wait for stop
wg.Wait()
}