-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogconfig.go
More file actions
107 lines (97 loc) · 3.19 KB
/
logconfig.go
File metadata and controls
107 lines (97 loc) · 3.19 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package zlog
import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
)
// Initialization log
func (z *ZapLogger) initLog() error {
encoderConfig := zapcore.EncoderConfig{
TimeKey: viper.GetString("zap.EncoderConfig.TimeKey"),
LevelKey: viper.GetString("zap.EncoderConfig.LevelKey"),
NameKey: viper.GetString("zap.EncoderConfig.NameKey"),
CallerKey: viper.GetString("zap.EncoderConfig.CallerKey"),
MessageKey: viper.GetString("zap.EncoderConfig.MessageKey"),
StacktraceKey: viper.GetString("zap.EncoderConfig.StacktraceKey"),
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder, // or LowercaseLevelEncoder lowercase encoder
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601TimeEncoder ISO8601 UTC time
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder, // full path encoder
}
atomicLevel := zap.NewAtomicLevelAt(filterZapAtomicLevelByViper()) // log Level
rotateLogger := lumberjack.Logger{
Filename: viper.GetString("zap.rotate.Filename"), // Log file path
MaxSize: viper.GetInt("zap.rotate.MaxSize"), // Maximum size of each log file Unit: M
MaxBackups: viper.GetInt("zap.rotate.MaxBackups"), // How many backups are saved in the log file
MaxAge: viper.GetInt("zap.rotate.MaxAge"), // How many days can the file be keep
Compress: viper.GetBool("zap.rotate.Compress"), // need compress
}
encoder := filterZapEncoder(encoderConfig)
core := zapcore.NewCore(
encoder, // Encoder configuration
zapcore.NewMultiWriteSyncer(
zapcore.AddSync(os.Stdout),
zapcore.AddSync(&rotateLogger),
), // Print to console and file
atomicLevel, // Log level
)
var filed zap.Option
if viper.GetBool("zap.FieldsAuto") {
filed = zap.Fields( //the initialization field
zap.String(viper.GetString("zap.Fields.Key"), viper.GetString("zap.Fields.Val")),
)
}
var logZap *zap.Logger
if viper.GetBool("zap.Development") {
if filed != nil {
logZap = zap.New(core, zap.AddCaller(), zap.Development(), filed)
} else {
logZap = zap.New(core, zap.AddCaller(), zap.Development())
}
} else {
if filed != nil {
logZap = zap.New(core, filed)
} else {
logZap = zap.New(core)
}
}
logZap.Debug("zlog init success")
Wrapper = logZap
Sugared = logZap.Sugar()
z.Log = logZap
z.Sugar = logZap.Sugar()
Logger = newLogger(z.ConfigPath, z.Log, z.Sugar)
return nil
}
func filterZapEncoder(encoderConfig zapcore.EncoderConfig) zapcore.Encoder {
var encoder zapcore.Encoder
zapEncoding := viper.GetString("zap.Encoding")
switch zapEncoding {
default:
encoder = zapcore.NewConsoleEncoder(encoderConfig)
case "json":
encoder = zapcore.NewJSONEncoder(encoderConfig)
case "console":
encoder = zapcore.NewConsoleEncoder(encoderConfig)
}
return encoder
}
func filterZapAtomicLevelByViper() zapcore.Level {
var atomViper zapcore.Level
switch viper.GetInt("zap.AtomicLevel") {
default:
atomViper = zap.InfoLevel
case -1:
atomViper = zap.DebugLevel
case 0:
atomViper = zap.InfoLevel
case 1:
atomViper = zap.WarnLevel
case 2:
atomViper = zap.ErrorLevel
}
return atomViper
}