-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig.go
191 lines (158 loc) · 4.93 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
191
package zapwriter
import (
"fmt"
"net/url"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Config configures a zapwriter.
//
// If SampleTick is defined, the Sample* parameters are passed to
// zapcore.NewSampler. See their documentation for details.
type Config struct {
Logger string `toml:"logger" json:"logger" comment:"handler name, default empty"`
File string `toml:"file" json:"file" comment:"'/path/to/filename', 'stderr', 'stdout', 'empty' (=='stderr'), 'none'"`
Level string `toml:"level" json:"level" comment:"'debug', 'info', 'warn', 'error', 'dpanic', 'panic', and 'fatal'"`
Encoding string `toml:"encoding" json:"encoding" comment:"'json' or 'console'"`
EncodingTime string `toml:"encoding-time" json:"encoding-time" comment:"'millis', 'nanos', 'epoch', 'iso8601'"`
EncodingDuration string `toml:"encoding-duration" json:"encoding-duration" comment:"'seconds', 'nanos', 'string'"`
SampleTick string `toml:"sample-tick" json:"sample-tick" comment:"passed to time.ParseDuration"`
SampleInitial int `toml:"sample-initial" json:"sample-initial" comment:"first n messages logged per tick"`
SampleThereafter int `toml:"sample-thereafter" json:"sample-thereafter" comment:"every m-th message logged thereafter per tick"`
}
func NewConfig() Config {
return Config{
File: "stderr",
Level: "info",
Encoding: "mixed",
EncodingTime: "iso8601",
EncodingDuration: "seconds",
}
}
func (c *Config) Clone() *Config {
clone := *c
return &clone
}
func (c *Config) Check() error {
_, err := c.build(true)
return err
}
func (c *Config) BuildLogger() (*zap.Logger, error) {
return c.build(false)
}
func (c *Config) encoder() (zapcore.Encoder, zap.AtomicLevel, error) {
u, err := url.Parse(c.File)
atomicLevel := zap.NewAtomicLevel()
if err != nil {
return nil, atomicLevel, err
}
level := u.Query().Get("level")
if level == "" {
level = c.Level
}
var zapLevel zapcore.Level
if err := zapLevel.UnmarshalText([]byte(level)); err != nil {
return nil, atomicLevel, err
}
atomicLevel.SetLevel(zapLevel)
encoding := u.Query().Get("encoding")
if encoding == "" {
encoding = c.Encoding
}
encodingTime := u.Query().Get("encoding-time")
if encodingTime == "" {
encodingTime = c.EncodingTime
}
encodingDuration := u.Query().Get("encoding-duration")
if encodingDuration == "" {
encodingDuration = c.EncodingDuration
}
var encoderTime zapcore.TimeEncoder
switch strings.ToLower(encodingTime) {
case "millis":
encoderTime = zapcore.EpochMillisTimeEncoder
case "nanos":
encoderTime = zapcore.EpochNanosTimeEncoder
case "epoch":
encoderTime = zapcore.EpochTimeEncoder
case "iso8601", "":
encoderTime = zapcore.ISO8601TimeEncoder
default:
return nil, atomicLevel, fmt.Errorf("unknown time encoding %#v", encodingTime)
}
var encoderDuration zapcore.DurationEncoder
switch strings.ToLower(encodingDuration) {
case "seconds", "":
encoderDuration = zapcore.SecondsDurationEncoder
case "nanos":
encoderDuration = zapcore.NanosDurationEncoder
case "string":
encoderDuration = zapcore.StringDurationEncoder
default:
return nil, atomicLevel, fmt.Errorf("unknown duration encoding %#v", encodingDuration)
}
encoderConfig := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "timestamp",
NameKey: "logger",
CallerKey: "caller",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: encoderTime,
EncodeDuration: encoderDuration,
}
var encoder zapcore.Encoder
switch strings.ToLower(encoding) {
case "mixed", "":
encoder = NewMixedEncoder(encoderConfig)
case "json":
encoder = zapcore.NewJSONEncoder(encoderConfig)
case "console":
encoder = zapcore.NewConsoleEncoder(encoderConfig)
default:
return nil, atomicLevel, fmt.Errorf("unknown encoding %#v", encoding)
}
return encoder, atomicLevel, nil
}
func (c *Config) build(checkOnly bool) (*zap.Logger, error) {
u, err := url.Parse(c.File)
if err != nil {
return nil, err
}
encoder, atomicLevel, err := c.encoder()
if err != nil {
return nil, err
}
if checkOnly {
return nil, nil
}
if strings.ToLower(u.Path) == "none" {
return zap.NewNop(), nil
}
ws, err := New(c.File)
if err != nil {
return nil, err
}
core, err := c.core(encoder, ws, atomicLevel)
if err != nil {
return nil, err
}
return zap.New(core), nil
}
func (c *Config) core(encoder zapcore.Encoder, ws zapcore.WriteSyncer, atomicLevel zap.AtomicLevel) (zapcore.Core, error) {
core := zapcore.NewCore(encoder, ws, atomicLevel)
if c.SampleTick != "" {
if c.SampleThereafter == 0 {
return nil, fmt.Errorf("a sample-thereafter value of 0 will cause a runtime divide-by-zero error in zap")
}
d, err := time.ParseDuration(c.SampleTick)
if err != nil {
return nil, err
}
core = zapcore.NewSampler(core, d, c.SampleInitial, c.SampleThereafter)
}
return core, nil
}