/
advanced.go
132 lines (115 loc) · 2.82 KB
/
advanced.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package log
import (
"io"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
type Level string
type Option func(c *Options)
const (
PanicLevel Level = "panic"
FatalLevel Level = "fatal"
ErrorLevel Level = "error"
WarnLevel Level = "warn"
InfoLevel Level = "info"
DebugLevel Level = "debug"
)
type Options struct {
Level Level
Out *OutputOption
AddCaller bool
AddCallerSkip int
}
type OutputOption struct {
OutPath io.Writer
ErrorPath io.Writer
}
func WithLevel(level Level) Option {
return func(c *Options) {
c.Level = level
}
}
func AddCaller() Option {
return func(c *Options) {
c.AddCaller = true
}
}
func AddCallerSkip(skipStep int) Option {
return func(c *Options) {
c.AddCallerSkip = skipStep
}
}
func WithOutputOption(f OutputOption) Option {
return func(c *Options) {
c.Out = &f
}
}
// WithRotate 是否使用滚动日志
// size 最大size,默认1M
// day 最长保留天数
// maxBackup 最多保留日志文件数量
// file 文件路径
func WithRotate(size, day, maxBackup int, file string) Option {
return func(c *Options) {
c.Out.OutPath = &lumberjack.Logger{
Filename: file,
MaxSize: size, // megabytes
MaxBackups: maxBackup,
MaxAge: day, //days
}
}
}
// WithErrorRotate 是否使用滚动日志
// size 最大size,默认1M
// day 最长保留天数
// maxBackup 最多保留日志文件数量
// file 文件路径
func WithErrorRotate(size, day, maxBackup int, file string) Option {
return func(c *Options) {
c.Out.ErrorPath = &lumberjack.Logger{
Filename: file,
MaxSize: size, // megabytes
MaxBackups: maxBackup,
MaxAge: day, //days
}
}
}
// Production 设置高级日志
func Production(opts ...Option) {
defaultOpts := &Options{
Level: DebugLevel,
Out: &OutputOption{
OutPath: os.Stderr,
ErrorPath: os.Stderr,
},
}
if len(opts) > 0 {
for _, v := range opts {
v(defaultOpts)
}
}
hijack(*defaultOpts)
}
func hijack(opts Options) {
l, _ := zapcore.ParseLevel(string(opts.Level))
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= l && lvl < zapcore.ErrorLevel
})
enc := zap.NewProductionEncoderConfig()
enc.EncodeTime = zapcore.RFC3339TimeEncoder
outEncoder := zapcore.NewJSONEncoder(enc)
c := zapcore.NewCore(outEncoder, zapcore.AddSync(opts.Out.OutPath), lowPriority)
errEncoder := zapcore.NewJSONEncoder(enc)
e := zapcore.NewCore(errEncoder, zapcore.AddSync(opts.Out.ErrorPath), highPriority)
core := zapcore.NewTee(c, e)
sugar := zap.New(core).Sugar()
if opts.AddCaller {
sugar = sugar.WithOptions(zap.AddCaller(), zap.AddCallerSkip(opts.AddCallerSkip))
}
DefaultLogger = &Logger{log: sugar, ws: opts.Out.OutPath}
}