/
zap.go
58 lines (54 loc) · 1.92 KB
/
zap.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
package zaplogger
import (
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Config mapstructure is for Viper to unmarshal
// TODO: add validation
type Config struct {
Development bool `mapstructure:"development"`
Level string `mapstructure:"level"`
Encoding string `mapstructure:"encoding"`
DisableCaller bool `mapstructure:"disable_caller"`
DisableStacktrace bool `mapstructure:"disable_stacktrace"`
DisableColor bool `mapstructure:"disable_color"`
OutputPaths []string `mapstructure:"output_paths"`
ErrorOutputPaths []string `mapstructure:"error_output_paths"`
}
// New returns initialised logger
func New(logCfg *Config) *zap.Logger {
zapCfg := zap.Config{Encoding: logCfg.Encoding,
Development: logCfg.Development,
DisableCaller: logCfg.DisableCaller,
DisableStacktrace: logCfg.DisableStacktrace,
ErrorOutputPaths: logCfg.ErrorOutputPaths,
OutputPaths: logCfg.OutputPaths,
}
var zapLvl zapcore.Level
if err := zapLvl.UnmarshalText([]byte(logCfg.Level)); err != nil {
fmt.Println("Incorrect logging.level value,", logCfg.Level)
os.Exit(1)
}
zapCfg.Level = zap.NewAtomicLevelAt(zapLvl)
zapCfg.EncoderConfig = zapcore.EncoderConfig{}
zapCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
zapCfg.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
if logCfg.DisableColor || logCfg.Encoding == "json" {
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
} else {
zapCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
zapCfg.EncoderConfig.TimeKey = "time"
zapCfg.EncoderConfig.MessageKey = "message"
zapCfg.EncoderConfig.LevelKey = "severity"
zapCfg.EncoderConfig.CallerKey = "caller"
zapCfg.EncoderConfig.EncodeDuration = zapcore.MillisDurationEncoder
logger, err := zapCfg.Build()
if err != nil {
fmt.Println("Failure initialising logger:", err)
os.Exit(1)
}
return logger
}