/
Logger.go
75 lines (65 loc) · 1.63 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package logger
import (
"runtime"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var Logger *zap.Logger
func Initialize(conf Config) {
initialFields := map[string]interface{}{
"app": &map[string]string{
"name": conf.AppName,
"arch": runtime.GOARCH,
"os": runtime.GOOS,
},
}
if conf.Version != "" {
m, _ := initialFields["app"].(map[string]string)
m["version"] = conf.Version
}
if conf.CommitSha != "" {
m, _ := initialFields["app"].(map[string]string)
m["commit"] = conf.CommitSha
}
config := zap.Config{
Level: zap.NewAtomicLevelAt(getLogLevel(conf.LogLevel)),
Development: conf.IsDevelopment,
Encoding: "json",
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
Sampling: nil,
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "severity",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
InitialFields: initialFields,
}
logger, err := config.Build()
if err != nil {
panic(err)
}
Logger = logger
}
func getLogLevel(level string) zapcore.Level {
switch level {
case "debug":
return zapcore.DebugLevel
case "info":
return zapcore.InfoLevel
case "warn":
return zapcore.WarnLevel
case "error":
return zapcore.ErrorLevel
}
return zapcore.DebugLevel
}