-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
98 lines (90 loc) · 2.72 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
package log
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewPlanetScaleConfigDefault creates an opinionated zap.Config while
// detecting encoding and Level.
func NewPlanetScaleConfigDefault() Config {
return NewPlanetScaleConfig(DetectEncoding(), DetectLevel())
}
// NewPlanetScaleConfig creates a zap.Config with the desired encoding and Level.
func NewPlanetScaleConfig(encoding string, level Level) Config {
// only buffer the JSON encoder
buffered := encoding == JSONEncoding
// override buffering if it's set explicitly
if v, isSet := DetectBuffering(); isSet {
buffered = v
}
return Config{
Level: zap.NewAtomicLevelAt(level),
Encoding: encoding,
Buffered: buffered,
}
}
var defaultEncoderConfig = zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
// Config is our logging configration
type Config struct {
Level zap.AtomicLevel
Encoding string
Buffered bool
NanoTime bool
}
// Build creates a Logger out of our Config.
// Note that this returns an error, but this doesn't actually
// error. This is maintained for compatibility with
// zapcore.Config{}.Build().
func (cfg Config) Build(opts ...zap.Option) (*Logger, error) {
var ws zapcore.WriteSyncer = os.Stderr
// XXX: the internal BufferedWriteSyncer in theory
// leaks a goroutine for the ticker to flush to stderr,
// but in practice, this shouldn't particularly be a concern
// since there we shouldn't be needing to create and destroy
// loggers at runtime. If this becomes an actual issue
// we might need to expose a way to get this
// BufferedWriteSyncer so the caller can call Stop() on it.
if cfg.Buffered {
ws = &zapcore.BufferedWriteSyncer{WS: ws}
}
log := zap.New(
zapcore.NewCore(
cfg.buildEncoder(),
ws,
cfg.Level,
),
zap.ErrorOutput(ws),
zap.AddCaller(),
zap.AddStacktrace(ErrorLevel),
)
if len(opts) > 0 {
log = log.WithOptions(opts...)
}
return log, nil
}
func (cfg Config) buildEncoder() zapcore.Encoder {
encoderConfig := defaultEncoderConfig
// we only suppport pretty or json
if cfg.Encoding == PrettyEncoding {
return NewPrettyEncoder(encoderConfig)
}
// NanoTime only applies when not using the pretty encoder, since
// nanosecond timestamps are, in fact, not pretty.
if cfg.NanoTime {
encoderConfig.EncodeTime = zapcore.EpochNanosTimeEncoder
}
return zapcore.NewJSONEncoder(encoderConfig)
}