-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
61 lines (55 loc) · 1.4 KB
/
logger.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
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"strings"
)
type Logger struct {
config zap.Config
base *zap.Logger
*zap.SugaredLogger
}
func (l *Logger) Init() {
l.config = zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
LevelKey: "level",
TimeKey: "timestamp",
NameKey: "logger",
MessageKey: "msg",
CallerKey: "",
StacktraceKey: "",
FunctionKey: zapcore.OmitKey,
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{
"stdout",
},
ErrorOutputPaths: []string{
"stderr",
},
}
l.base, _ = l.config.Build()
l.SugaredLogger = l.base.Sugar()
}
func (l *Logger) SwitchLevel(level string) {
if strings.EqualFold(l.config.Level.String(), level) {
l.Infow("switching logging level", "not needed, same")
return
}
l.Infow("switching logging level", "from", l.config.Level.String(), "to", level)
err := l.config.Level.UnmarshalText([]byte(level))
if err != nil {
l.Fatalw("failed to switch level", "from", l.config.Level, "to", level)
}
}