/
config.go
190 lines (169 loc) · 4.56 KB
/
config.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package zaplogger
import (
"fmt"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
LevelDebug = "debug"
LevelInfo = "info"
LevelWarn = "warn"
LevelError = "error"
)
const (
EncoderConsole = "console"
EncoderJSON = "json"
)
const (
TimeEncoderISO8601 = "iso8601"
TimeEncoderMillis = "millis"
TimeEncoderNanos = "nano"
TimeEncoderEpoch = "epoch"
TimeEncoderDefault = "default"
)
var Config ZapConfig
var config *zapConfig
type ZapConfig struct {
Development bool `json:"development,omitempty" yaml:"development,omitempty"`
Encoder string `json:"encoder,omitempty" yaml:"encoder,omitempty"`
Level string `json:"level,omitempty" yaml:"level,omitempty"`
StackLevel string `json:"stack_level,omitempty" yaml:"stack_level,omitempty"`
Sample bool `json:"sample,omitempty" yaml:"sample,omitempty"`
TimeEncoding string `json:"time_encoding,omitempty" yaml:"time_encoding,omitempty"`
}
type zapConfig struct {
level zap.AtomicLevel
stackLevel zapcore.Level
encoder zapcore.Encoder
opts []zap.Option
sample bool
}
type encoderConfigFunc func(*zapcore.EncoderConfig)
type encoderFunc func(...encoderConfigFunc) zapcore.Encoder
func NewConfig(c ZapConfig) (*zapConfig, error) {
var zc zapConfig
var eFunc encoderFunc
// If development is enabled, use the default development config;
// otherwise, use the default production config.
if c.Development {
eFunc, _ = getEncoder(EncoderConsole)
zc.level = zap.NewAtomicLevelAt(zap.DebugLevel)
zc.opts = append(zc.opts, zap.Development())
zc.sample = false
zc.stackLevel = zap.WarnLevel
} else {
eFunc, _ = getEncoder(EncoderJSON)
zc.level = zap.NewAtomicLevelAt(zap.InfoLevel)
zc.sample = true
zc.stackLevel = zap.ErrorLevel
}
// If Level is set, override the default Level
if c.Level != "" {
lvl, err := getLevel(c.Level)
if err != nil {
return nil, err
}
zc.level = zap.NewAtomicLevelAt(lvl)
}
// If StackLevel is set, override the default StackLevel
if c.StackLevel != "" {
lvl, err := getLevel(c.StackLevel)
if err != nil {
return nil, err
}
zc.stackLevel = lvl
}
zc.opts = append(zc.opts, zap.AddStacktrace(zc.stackLevel))
// If Encoder is set, override the default Encoder
if c.Encoder != "" {
f, err := getEncoder(c.Encoder)
if err != nil {
return nil, err
}
eFunc = f
}
// Set TimeEncoding, use "2006-01-02 15:04:05" by default
var ecFuncs []encoderConfigFunc
if c.TimeEncoding != "" {
tec, err := getTimeEncoder(c.TimeEncoding)
if err != nil {
return nil, err
}
ecFuncs = append(ecFuncs, withTimeEncoding(tec))
} else {
tec, _ := getTimeEncoder(TimeEncoderDefault)
ecFuncs = append(ecFuncs, withTimeEncoding(tec))
}
zc.encoder = eFunc(ecFuncs...)
zc.sample = c.Sample
if zc.level.Level() <= -1 {
zc.sample = false
}
return &zc, nil
}
func getLevel(l string) (zapcore.Level, error) {
lower := strings.ToLower(l)
var lvl zapcore.Level
switch lower {
case LevelDebug:
lvl = zapcore.DebugLevel
case LevelInfo:
lvl = zapcore.InfoLevel
case LevelWarn:
lvl = zapcore.WarnLevel
case LevelError:
lvl = zapcore.ErrorLevel
default:
return lvl, fmt.Errorf("invalid log level \"%s\"", l)
}
return lvl, nil
}
func getEncoder(ec string) (encoderFunc, error) {
lower := strings.ToLower(ec)
switch lower {
case EncoderConsole:
return func(ecfs ...encoderConfigFunc) zapcore.Encoder {
encoderConfig := zap.NewDevelopmentEncoderConfig()
for _, f := range ecfs {
f(&encoderConfig)
}
return zapcore.NewConsoleEncoder(encoderConfig)
}, nil
case EncoderJSON:
return func(ecfs ...encoderConfigFunc) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
for _, f := range ecfs {
f(&encoderConfig)
}
return zapcore.NewJSONEncoder(encoderConfig)
}, nil
default:
return nil, fmt.Errorf("invalid encoder \"%s\"", ec)
}
}
func getTimeEncoder(tec string) (zapcore.TimeEncoder, error) {
lower := strings.ToLower(tec)
switch lower {
case TimeEncoderISO8601:
return zapcore.ISO8601TimeEncoder, nil
case TimeEncoderMillis:
return zapcore.EpochMillisTimeEncoder, nil
case TimeEncoderNanos:
return zapcore.EpochNanosTimeEncoder, nil
case TimeEncoderEpoch:
return zapcore.EpochTimeEncoder, nil
case TimeEncoderDefault:
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}, nil
default:
return nil, fmt.Errorf("invalid time encoder \"%s\"", tec)
}
}
func withTimeEncoding(tec zapcore.TimeEncoder) encoderConfigFunc {
return func(ec *zapcore.EncoderConfig) {
ec.EncodeTime = tec
}
}