/
logger.go
36 lines (32 loc) · 989 Bytes
/
logger.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
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Config struct {
Path string `yaml:"path"`
// If Path is a file, Mode will determine how the log file is managed.
// FileModeAppend is the default if value is undefined.
Mode FileMode `yaml:"mode,omitempty"`
Level zapcore.Level `yaml:"level"`
DevMode bool `yaml:"devmode"`
}
func New(conf Config) (*zap.Logger, error) {
w, err := OpenFile(conf.Path, conf.Mode)
if err != nil {
return nil, err
}
core := zapcore.NewCore(jsonEncoder(), w, conf.Level)
opts := []zap.Option{zap.AddStacktrace(zapcore.WarnLevel)}
// If the development mode is on, calls to logger.DPanic will cause a panic
// whereas in production would result in an error.
if conf.DevMode {
opts = append(opts, zap.Development())
}
return zap.New(core, opts...), nil
}
func jsonEncoder() zapcore.Encoder {
conf := zap.NewProductionEncoderConfig()
conf.CallerKey = ""
return zapcore.NewJSONEncoder(conf)
}