-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
99 lines (90 loc) · 2.74 KB
/
config.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package zap_mate
import (
"os"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
//filaname : config files of zap
//section : Config options
func NewZapLogger(filename, section string) *zap.Logger {
config := viper.New()
config.SetConfigFile(filename)
if err := config.ReadInConfig(); err != nil {
panic(err)
}
logSec := config.Sub(section)
return zap.New(zapcore.NewCore(
func() zapcore.Encoder {
encoderConfig := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
TimeKey: "time",
NameKey: "logger",
CallerKey: "file",
FunctionKey: "func",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout(logSec.GetString("time-format")),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if logSec.GetBool("level-color") {
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
if logSec.GetString("encoder") == "json" {
return zapcore.NewJSONEncoder(encoderConfig)
}
return zapcore.NewConsoleEncoder(encoderConfig)
}(),
zapcore.NewMultiWriteSyncer(
func() (writer []zapcore.WriteSyncer) {
if logSec.GetString("file-name")!="" {
writer = append(writer,
zapcore.AddSync(
&lumberjack.Logger{
Filename: logSec.GetString("file-name"),
MaxSize: logSec.GetInt("max-size"),
MaxBackups: logSec.GetInt("max-count"),
MaxAge: logSec.GetInt("max-age"),
Compress: false,
LocalTime: true,
}),
)
}
if logSec.GetBool("stdout") {
writer = append(writer, zapcore.AddSync(os.Stdout))
}
return writer
}()...,
),
//DISABLE
//zapcore.NewMultiWriteSyncer(
//
// func() []zapcore.WriteSyncer {
// hook, err := rotatelogs.New(
// logSec.GetString("file-name")+".%Y-%m-%d",
// rotatelogs.WithLinkName(logSec.GetString("file-name")),
// rotatelogs.WithMaxAge(time.Hour*24*time.Duration(logSec.GetInt("max-age"))),
// rotatelogs.WithRotationTime(time.Hour*24),
// rotatelogs.WithRotationSize(logSec.GetInt64("max-size")*1024*1024),
// //rotatelogs.WithRotationCount(logSec.GetUint("max-count")),
// )
// if err != nil {
// panic(hook)
// }
// writes := []zapcore.WriteSyncer{zapcore.AddSync(hook)}
// if logSec.GetBool("stdout") {
// writes = append(writes, zapcore.AddSync(os.Stdout))
// }
// return writes
// }()...,
//),
zapcore.Level(logSec.GetInt("level")),
),
zap.AddStacktrace(zapcore.Level(logSec.GetInt("stack-trace-level"))),
zap.WithCaller(logSec.GetBool("caller")),
)
}