-
Notifications
You must be signed in to change notification settings - Fork 0
/
zapconfig.go
140 lines (121 loc) · 2.78 KB
/
zapconfig.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
package log
import (
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type zapconfiger struct {
env string
name string
level string
encoding string
skip int
c zap.Config
ec zapcore.EncoderConfig
opts []zap.Option
}
func newZapConfig() Configer {
return &zapconfiger{
skip: 1,
c: zap.Config{},
opts: make([]zap.Option, 0, 0),
}
}
func (zc *zapconfiger) Level(level string) Configer {
if level == "" {
zc.level = DEBUG
} else {
zc.level = level
}
return zc
}
func (zc *zapconfiger) Name(name string) Configer {
if name == "" {
zc.name = "zaplog"
} else {
zc.name = name
}
return zc
}
func (zc *zapconfiger) Env(env string) Configer {
if env == "" {
zc.env = DEV
} else {
zc.env = env
}
return zc
}
func (zc *zapconfiger) AddCallerSkip(skip int) Configer {
zc.skip = skip
return zc
}
func (zc *zapconfiger) Build() (Logger, error) {
switch zc.env {
case PRD:
zc.c = productConfig()
default:
zc.c = defaultConfig()
switch zc.level {
case INFO:
zc.c.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
case WARN:
zc.c.Level = zap.NewAtomicLevelAt(zap.WarnLevel)
case ERROR:
zc.c.Level = zap.NewAtomicLevelAt(zap.ErrorLevel)
case FATAL:
zc.c.Level = zap.NewAtomicLevelAt(zap.FatalLevel)
case DEBUG:
zc.c.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
}
zc.opts = append(zc.opts, zap.AddCallerSkip(zc.skip))
return zc.build()
}
func (zc *zapconfiger) build() (Logger, error) {
x := new(zlog)
x.zl, x.err = zc.c.Build(zc.opts...)
log = x
return x, x.err
}
func defaultEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: rfc3339TimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func defaultConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: true,
Encoding: "json",
EncoderConfig: defaultEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}
func productConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(zap.WarnLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: defaultEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}
func rfc3339TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339Nano))
}