-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
68 lines (52 loc) · 1.88 KB
/
main.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
package main
import (
"errors"
"fmt"
"go.uber.org/zap"
"strings"
"time"
)
func main() {
logger, _ := zap.NewProduction() //生产环境
//logger,_ := zap.NewDevelopment() //开发环境
defer logger.Sync() // flushes buffer, if any
url := "https://www.baidu.com"
// 方式一 :兼容Printf
sugarPrint(logger)
// 方式二 :无反射机制
logger.Info("failed to fetch url3",
zap.String("url", url),
zap.Int("num", 3))
// 结果键值对方式{"level":"info","ts":1625733829.883981,"caller":"cosole/consumer.go:46","msg":"failed to fetch url","url":"https://www.baidu.com","num":3}
// 错误栈帧调用
errorStacktraceDemo(logger)
}
func sugarPrint(logger *zap.Logger) {
url := "https://www.google.com"
sugar := logger.Sugar()
sugar.Infow("failed to fetch URL1",
// Structured context as loosely typed key-value pairs.
"url", url,
"attempt", 3,
"backoff", time.Second,
)
// {"level":"info","ts":1625733799.522482,"caller":"cosole/consumer.go:36","msg":"failed to fetch URL1","url":"https://www.baidu.com","attempt":3,"backoff":1}
sugar.Infof("Failed to fetch URL2: %s", url)
// {"level":"info","ts":1625733829.883971,"caller":"cosole/consumer.go:43","msg":"Failed to fetch URL2: https://www.baidu.com"}
}
// 错误栈帧查看
func errorStacktraceDemo(logger *zap.Logger) {
// 抛出错误
logger.Info("抛出错误errorField", zap.Error(errors.New("demo err")))
// 定义错误
fmt.Println(strings.Repeat("---", 30))
fmt.Println(zap.Stack("default stack1").String)
fmt.Println(strings.Repeat("===", 30))
fmt.Println(zap.StackSkip("跳过skip前 2个栈", 2).String)
fmt.Println(strings.Repeat("***", 30))
logger.Info("栈追踪默认", zap.Stack("默认栈1"))
fmt.Println(strings.Repeat("###", 30))
logger.Info("栈追踪 跳过 2层", zap.StackSkip("skip 2", 2))
fmt.Println(strings.Repeat("$$$", 30))
logger.Error("栈追踪 默认", zap.Stack("默认栈2"))
}