-
Notifications
You must be signed in to change notification settings - Fork 14
/
zap.go
63 lines (54 loc) · 1.44 KB
/
zap.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
package kafka
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type logger struct {
*zap.SugaredLogger
}
func NewZapLogger(level LogLevel) LoggerInterface {
if level == "" {
level = LogLevelInfo
}
l, _ := newLogger(level)
return &logger{l.Sugar()}
}
func (l *logger) With(args ...interface{}) LoggerInterface {
if len(args) > 0 {
return &logger{l.SugaredLogger.With(args...)}
}
return l
}
func newLogger(level LogLevel) (*zap.Logger, error) {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "sourceLocation",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02T15:04:05.999Z"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
// default log level is Info
lvl := zapcore.InfoLevel
_ = lvl.Set(string(level))
const initial = 100
config := zap.Config{
Level: zap.NewAtomicLevelAt(lvl),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: initial,
Thereafter: initial,
},
Encoding: "json",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
return config.Build(zap.AddStacktrace(zap.FatalLevel))
}