-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
64 lines (57 loc) · 1.47 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
59
60
61
62
63
64
package logger
import (
"context"
"os"
"github.com/goriller/ginny-util/graceful"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
std *zap.Logger
)
func init() {
// you can use `export LOG_PATH=logs/log.log` to set log output to a file
logFilePath := os.Getenv("LOG_PATH")
level := zap.NewAtomicLevel()
logLevel := os.Getenv("LOG_LEVEL")
if logLevel != "" {
err := level.UnmarshalText([]byte(logLevel))
if err != nil {
level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
} else {
level = zap.NewAtomicLevelAt(zap.DebugLevel)
}
cores := make([]zapcore.Core, 0, 1)
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = "time"
encoderCfg.CallerKey = "caller"
encoderCfg.EncodeTime = zapcore.RFC3339NanoTimeEncoder
if logFilePath != "" {
fw := zapcore.AddSync(&lumberjack.Logger{
Filename: logFilePath,
MaxSize: 1024, // megabytes
MaxBackups: 3,
MaxAge: 3, // days
})
// file core 采用jsonEncoder
je := zapcore.NewJSONEncoder(encoderCfg)
cores = append(cores, zapcore.NewCore(je, fw, level))
} else {
// stdout core
cw := zapcore.Lock(os.Stdout)
ce := zapcore.NewJSONEncoder(encoderCfg)
cores = append(cores, zapcore.NewCore(ce, cw, level))
}
core := zapcore.NewTee(cores...)
opt := []zap.Option{
zap.AddCaller(),
zap.AddCallerSkip(1),
}
std = zap.New(core, opt...)
zap.ReplaceGlobals(std)
graceful.AddCloser(func(ctx context.Context) error {
return std.Sync()
})
}