This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
/
zap.go
208 lines (162 loc) · 4.46 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package zap
import (
"github.com/micro-in-cn/tutorials/microservice-in-micro/part5/basic"
"github.com/micro-in-cn/tutorials/microservice-in-micro/part5/basic/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"path/filepath"
"sync"
)
var (
l *Logger
sp = string(filepath.Separator)
errWS, warnWS, infoWS, debugWS zapcore.WriteSyncer // IO输出
debugConsoleWS = zapcore.Lock(os.Stdout) // 控制台标准输出
errorConsoleWS = zapcore.Lock(os.Stderr)
)
func init() {
l = &Logger{
Opts: &Options{},
}
basic.Register(initLogger)
}
type Logger struct {
*zap.Logger
sync.RWMutex
Opts *Options `json:"opts"`
zapConfig zap.Config
inited bool
}
func initLogger() {
l.Lock()
defer l.Unlock()
if l.inited {
l.Info("[initLogger] logger Inited")
return
}
l.loadCfg()
l.init()
l.Info("[initLogger] zap plugin initializing completed")
l.inited = true
}
// GetLogger returns logger
func GetLogger() (ret *Logger) {
return l
}
func (l *Logger) init() {
l.setSyncers()
var err error
l.Logger, err = l.zapConfig.Build(l.cores())
if err != nil {
panic(err)
}
defer l.Logger.Sync()
}
func (l *Logger) loadCfg() {
c := config.C()
err := c.Path("zap", l.Opts)
if err != nil {
panic(err)
}
if l.Opts.Development {
l.zapConfig = zap.NewDevelopmentConfig()
} else {
l.zapConfig = zap.NewProductionConfig()
}
// application log output path
if l.Opts.OutputPaths == nil || len(l.Opts.OutputPaths) == 0 {
l.zapConfig.OutputPaths = []string{"stdout"}
}
// error of zap-self log
if l.Opts.ErrorOutputPaths == nil || len(l.Opts.ErrorOutputPaths) == 0 {
l.zapConfig.OutputPaths = []string{"stderr"}
}
// 默认输出到程序运行目录的logs子目录
if l.Opts.LogFileDir == "" {
l.Opts.LogFileDir, _ = filepath.Abs(filepath.Dir(filepath.Join(".")))
l.Opts.LogFileDir += sp + "logs" + sp
}
if l.Opts.AppName == "" {
l.Opts.AppName = "app"
}
if l.Opts.ErrorFileName == "" {
l.Opts.ErrorFileName = "error.log"
}
if l.Opts.WarnFileName == "" {
l.Opts.WarnFileName = "warn.log"
}
if l.Opts.InfoFileName == "" {
l.Opts.InfoFileName = "info.log"
}
if l.Opts.DebugFileName == "" {
l.Opts.DebugFileName = "debug.log"
}
if l.Opts.MaxSize == 0 {
l.Opts.MaxSize = 50
}
if l.Opts.MaxBackups == 0 {
l.Opts.MaxBackups = 3
}
if l.Opts.MaxAge == 0 {
l.Opts.MaxAge = 30
}
}
func (l *Logger) setSyncers() {
f := func(fN string) zapcore.WriteSyncer {
return zapcore.AddSync(&lumberjack.Logger{
Filename: l.Opts.LogFileDir + sp + l.Opts.AppName + "-" + fN,
MaxSize: l.Opts.MaxSize,
MaxBackups: l.Opts.MaxBackups,
MaxAge: l.Opts.MaxAge,
Compress: true,
LocalTime: true,
})
}
errWS = f(l.Opts.ErrorFileName)
warnWS = f(l.Opts.WarnFileName)
infoWS = f(l.Opts.InfoFileName)
debugWS = f(l.Opts.DebugFileName)
return
}
func (l *Logger) cores() zap.Option {
fileEncoder := zapcore.NewJSONEncoder(l.zapConfig.EncoderConfig)
consoleEncoder := zapcore.NewConsoleEncoder(l.zapConfig.EncoderConfig)
errPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl > zapcore.WarnLevel && zapcore.WarnLevel-l.zapConfig.Level.Level() > -1
})
warnPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl == zapcore.WarnLevel && zapcore.WarnLevel-l.zapConfig.Level.Level() > -1
})
infoPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl == zapcore.InfoLevel && zapcore.InfoLevel-l.zapConfig.Level.Level() > -1
})
debugPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl == zapcore.DebugLevel && zapcore.DebugLevel-l.zapConfig.Level.Level() > -1
})
cores := []zapcore.Core{
// region 日志文件
// error 及以上
zapcore.NewCore(fileEncoder, errWS, errPriority),
// warn
zapcore.NewCore(fileEncoder, warnWS, warnPriority),
// info
zapcore.NewCore(fileEncoder, infoWS, infoPriority),
// debug
zapcore.NewCore(fileEncoder, debugWS, debugPriority),
// endregion
// region 控制台
// 错误及以上
zapcore.NewCore(consoleEncoder, errorConsoleWS, errPriority),
// 警告
zapcore.NewCore(consoleEncoder, debugConsoleWS, warnPriority),
// info
zapcore.NewCore(consoleEncoder, debugConsoleWS, infoPriority),
// debug
zapcore.NewCore(consoleEncoder, debugConsoleWS, debugPriority),
// endregion
}
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
})
}