forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
58 lines (48 loc) · 1.36 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
package slog_test
import (
"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")
}