-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
62 lines (48 loc) · 1.36 KB
/
log.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
package log
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func New(cfg Config) *zap.Logger {
return zap.New(
zapcore.NewCore(getEncoder(cfg), getWriteSyncer(), getLoggerLevel(cfg)),
getOptions(cfg)...,
)
}
func getEncoder(cfg Config) zapcore.Encoder { //nolint:ireturn
var encoderConfig zapcore.EncoderConfig
if cfg.Production {
encoderConfig = zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
} else {
encoderConfig = zap.NewDevelopmentEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
var encoder zapcore.Encoder
if cfg.Encoding == "console" {
encoder = zapcore.NewConsoleEncoder(encoderConfig)
} else {
encoder = zapcore.NewJSONEncoder(encoderConfig)
}
return encoder
}
func getWriteSyncer() zapcore.WriteSyncer { //nolint:ireturn
return zapcore.Lock(os.Stdout)
}
func getLoggerLevel(cfg Config) zap.AtomicLevel {
var level zapcore.Level
if err := level.Set(cfg.Level); err != nil {
return zap.NewAtomicLevelAt(zapcore.DebugLevel)
}
return zap.NewAtomicLevelAt(level)
}
func getOptions(cfg Config) []zap.Option {
options := make([]zap.Option, 0)
if !cfg.Production {
options = append(options, zap.AddCaller())
options = append(options, zap.AddStacktrace(zap.ErrorLevel))
}
return options
}