forked from sealdice/sealdice-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
51 lines (39 loc) · 1.23 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
package main
import (
"os"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func MainLoggerInit(path string, enableConsoleLog bool) {
lumlog := &lumberjack.Logger{
Filename: path,
MaxSize: 10, // megabytes
MaxBackups: 3, // number of log files
MaxAge: 7, // days
}
encoder := getEncoder()
cores := []zapcore.Core{
zapcore.NewCore(encoder, zapcore.AddSync(lumlog), zapcore.DebugLevel),
}
if enableConsoleLog {
pe2 := zap.NewProductionEncoderConfig()
pe2.EncodeTime = zapcore.ISO8601TimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(pe2)
cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.InfoLevel))
}
core := zapcore.NewTee(cores...)
loggerRaw := zap.New(core, zap.AddCaller())
defer func(loggerRaw *zap.Logger) {
_ = loggerRaw.Sync()
}(loggerRaw) // flushes buffer, if any
logger = loggerRaw.Sugar()
logger.Infow("核心日志开始记录")
}
var logger *zap.SugaredLogger
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
return zapcore.NewConsoleEncoder(encoderConfig)
}