/
logger.go
55 lines (47 loc) · 1.36 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package logger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const Loglevel = zap.ErrorLevel
var zapLogger *zap.Logger
func NewProductionEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "", // disable printing timestamp "ts"
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func init() {
cfg := zap.NewProductionConfig()
cfg.Encoding = "json"
cfg.Level = zap.NewAtomicLevelAt(Loglevel)
cfg.InitialFields = map[string]interface{}{"app": "store-cli"}
cfg.OutputPaths = []string{"stdout"}
cfg.ErrorOutputPaths = []string{"stderr"}
cfg.DisableStacktrace = true
cfg.DisableCaller = true
cfg.EncoderConfig = NewProductionEncoderConfig()
zapLogger, _ = cfg.Build()
defer func() { _ = zapLogger.Sync() }()
}
func Info(msg string) {
zapLogger.Info(msg)
}
func Warn(err ...interface{}) {
msg := append([]interface{}{"IGNORE,"}, err...)
zapLogger.Warn(fmt.Sprintf("%v", msg))
}
func Error(err error) error {
zapLogger.Error(fmt.Sprintf("%v", err))
return fmt.Errorf(fmt.Sprintf("%v", err))
}