-
Notifications
You must be signed in to change notification settings - Fork 0
/
out.go
170 lines (152 loc) · 4.69 KB
/
out.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"time"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main1() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
defer logger.Sync()
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", "www.google.com"),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
topicDebugging := zapcore.AddSync(ioutil.Discard)
topicErrors := zapcore.AddSync(ioutil.Discard)
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
kafkaEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core := zapcore.NewTee(
zapcore.NewCore(kafkaEncoder, topicErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(kafkaEncoder, topicDebugging, lowPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logger = zap.New(core)
defer logger.Sync()
logger.Error("found error ")
logger.Info("constructed a logger")
rawJSON := []byte(`{
"level": "error",
"encoding": "console",
"outputPaths": ["stdout", "/tmp/logs"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"timeKey": "time",
"messageKey": "message",
"levelKey": "level",
"callerEncoder":"callerEncoder",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
panic(err)
}
cfg.DisableCaller = false
cfg.EncoderConfig.EncodeTime = syslogTimeEncoder
cfg.EncoderConfig.EncodeLevel = customLevelEncoder
cfg.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
logger2, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger2.Sync()
logger2.Info("logger2 construction succeeded")
logger2.Error("second error ...")
cfg2 := zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
DisableCaller: false,
DisableStacktrace: false,
Sampling: nil,
Encoding: "console",
// Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
// Keys can be anything except the empty string.
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: customLevelEncoder,
EncodeTime: syslogTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stdout", "/tmp/logs"},
ErrorOutputPaths: []string{"stderr"},
InitialFields: nil,
}
logger3, err := cfg2.Build()
logger3.Info("test")
// logger3.Error("test")
// ===============================
terminalEncoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
// Keys can be anything except the empty string.
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: customLevelEncoder,
EncodeTime: syslogTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
})
terminalOutput := zapcore.AddSync(os.Stderr)
fileEncoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: filelogTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
})
fileOutput := zapcore.AddSync(&lumberjack.Logger{
Filename: "logs.json",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
InfoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.InfoLevel
})
ErrorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
core2 := zapcore.NewTee(
zapcore.NewCore(terminalEncoder, terminalOutput, InfoLevel),
zapcore.NewCore(fileEncoder, fileOutput, ErrorLevel),
)
logger4 := zap.New(core2, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
defer logger4.Sync()
logger4.Error("logger4.Error ")
logger4.Info("logger4.info")
}