-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
307 lines (260 loc) · 8.31 KB
/
log.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package wzap
import (
"fmt"
"sync"
)
var (
loggers = make(map[string]*Logger)
logger = New(
WithLevelMask(DebugLevel|InfoLevel|WarnLevel|ErrorLevel|FatalLevel),
WithColorful(true),
WithAsync(false),
WithPrefix("APP]>"),
)
defaultFileSuffix = "log"
mu sync.RWMutex
)
func init() {
loggers = map[string]*Logger{
"default": logger,
}
}
// Writer defines logger's writer.
type Writer interface {
Sync() error
Level() int
Print(int, string, ...interface{})
Printf(int, string, ...interface{})
CheckErr(error, func(string, ...interface{})) bool
}
// New constructs a new Logger instance.
func New(opts ...Option) *Logger {
var options Options
for _, option := range opts {
option(&options)
}
logger := &Logger{
options: &options,
writers: make([]Writer, 0),
}
logger.init()
return logger
}
// Default returns a default Logger instance
func Default(name string) *Logger {
return New(
WithPath(name),
WithLevelCombo("Info|Warn|Error|Panic|Fatal"),
)
}
// SetDefaultLogger sets default logger with provided logger.
func SetDefaultLogger(l *Logger) {
mu.Lock()
logger = l
loggers["default"] = l
mu.Unlock()
}
// SetDefaultFileSuffix sets default file suffix.
func SetDefaultFileSuffix(suffix string) {
defaultFileSuffix = suffix
}
// Log returns logger with provided name.
func Log(name string) *Logger {
mu.RLock()
defer mu.RUnlock()
if _, ok := loggers[name]; !ok {
return loggers["default"]
}
return loggers[name]
}
// Register registers logger into loggers.
func Register(name string, logger *Logger) {
mu.Lock()
defer mu.Unlock()
loggers[name] = logger
}
func (l *Logger) init() {
if len(l.options.writers) == 0 {
l.options.fs = append(defaultFields, l.options.fs...)
l.addWriter(*l.options)
} else {
for _, wopt := range l.options.writers {
wopt.fs = append(defaultFields, wopt.fs...)
l.addWriter(wopt)
}
}
}
func (l *Logger) addWriter(o Options) {
if o.path != "" {
l.writers = append(l.writers, NewZapWriter(o.path, o.level, o.fs))
} else if o.name != "" {
l.writers = append(l.writers, NewZapWriter(fmt.Sprintf("%s.%s", o.name, defaultFileSuffix), o.level, o.fs))
} else {
writer := NewConsoleWriter(o.level, o.color, o.fs)
writer.SetAsync(o.async)
writer.SetPrefix(o.prefix)
l.writers = append(l.writers, writer)
}
}
// Logger is an interface supports printf-style and structured-style logging.
type Logger struct {
options *Options
writers []Writer
}
// Sync syncs logger messages.
func Sync() {
logger.Sync()
}
// Sync syncs logger messages.
func (l Logger) Sync() {
for _, w := range l.writers {
if err := w.Sync(); err != nil {
fmt.Printf("logger sync fail, %#v, %s\n", w, err.Error())
}
}
}
// Debug logs debug level messages with default logger.
func Debug(msg string, args ...interface{}) {
logger.Print(DebugLevel, msg, args...)
}
// Debug logs debug level messages.
func (l *Logger) Debug(msg string, args ...interface{}) {
l.Print(DebugLevel, msg, args...)
}
// Debugf logs debug level messages with default logger in printf-style.
func Debugf(format string, args ...interface{}) {
logger.Printf(DebugLevel, format, args...)
}
// Debugf logs debug level messages in printf-style.
func (l Logger) Debugf(format string, args ...interface{}) {
l.Printf(DebugLevel, format, args...)
}
// Info logs Info level messages with default logger in structured-style.
func Info(msg string, args ...interface{}) {
logger.Print(InfoLevel, msg, args...)
}
// Info logs Info level messages in structured-style.
func (l *Logger) Info(msg string, args ...interface{}) {
l.Print(InfoLevel, msg, args...)
}
// Infof logs Info level messages with default logger in printf-style.
func Infof(format string, args ...interface{}) {
logger.Printf(InfoLevel, format, args...)
}
// Infof logs Info level messages in printf-style.
func (l Logger) Infof(format string, args ...interface{}) {
l.Printf(InfoLevel, format, args...)
}
// Warn logs Warn level messages with default logger in structured-style.
func Warn(msg string, args ...interface{}) {
logger.Print(WarnLevel, msg, args...)
}
// Warn logs Warn level messages in structured-style.
func (l Logger) Warn(msg string, args ...interface{}) {
l.Print(WarnLevel, msg, args...)
}
// Warnf logs Warn level messages with default logger in printf-style.
func Warnf(format string, args ...interface{}) {
logger.Printf(WarnLevel, format, args...)
}
// Warnf logs Warn level messages in printf-style.
func (l Logger) Warnf(format string, args ...interface{}) {
l.Printf(WarnLevel, format, args...)
}
// Error logs Error level messages with default logger in structured-style.
// Notice: additional stack will be added into messages.
func Error(msg string, args ...interface{}) {
logger.Print(ErrorLevel, msg, args...)
}
// Error logs Error level messages in structured-style.
// Notice: additional stack will be added into messages.
func (l Logger) Error(msg string, args ...interface{}) {
l.Print(ErrorLevel, msg, args...)
}
// Errorf logs Error level messages with default logger in printf-style.
// Notice: additional stack will be added into messages.
func Errorf(format string, args ...interface{}) {
logger.Printf(ErrorLevel, format, args...)
}
// Errorf logs Error level messages in printf-style.
// Notice: additional stack will be added into messages.
func (l Logger) Errorf(format string, args ...interface{}) {
l.Printf(ErrorLevel, format, args...)
}
// Panic logs Panic level messages with default logger in structured-style.
// Notice: additional stack will be added into messages, meanwhile logger will panic.
func Panic(msg string, args ...interface{}) {
logger.Print(PanicLevel, msg, args...)
}
// Panic logs Panic level messages in structured-style.
// Notice: additional stack will be added into messages, meanwhile logger will panic.
func (l Logger) Panic(msg string, args ...interface{}) {
l.Print(PanicLevel, msg, args...)
}
// Panicf logs Panicf level messages with default logger in printf-style.
// Notice: additional stack will be added into messages, meanwhile logger will panic.
func Panicf(format string, args ...interface{}) {
logger.Printf(PanicLevel, format, args...)
}
// Panicf logs Panicf level messages in printf-style.
// Notice: additional stack will be added into messages, meanwhile logger will panic.
func (l Logger) Panicf(format string, args ...interface{}) {
l.Printf(PanicLevel, format, args...)
}
// Fatal logs Fatal level messages with default logger in structured-style.
// Notice: additional stack will be added into messages, then calls os.Exit(1).
func Fatal(msg string, args ...interface{}) {
logger.Print(FatalLevel, msg, args...)
}
// Fatal logs Fatal level messages in structured-style.
// Notice: additional stack will be added into messages, then calls os.Exit(1).
func (l Logger) Fatal(msg string, args ...interface{}) {
l.Print(FatalLevel, msg, args...)
}
// Fatalf logs Fatalf level messages with default logger in printf-style.
// Notice: additional stack will be added into messages, then calls os.Exit(1).
func Fatalf(format string, args ...interface{}) {
logger.Printf(FatalLevel, format, args...)
}
// Fatalf logs Fatalf level messages in printf-style.
// Notice: additional stack will be added into messages, then calls os.Exit(1).
func (l Logger) Fatalf(format string, args ...interface{}) {
l.Printf(FatalLevel, format, args...)
}
// Print logs messages with provided level in structured-style.
func (l *Logger) Print(level int, msg string, args ...interface{}) {
for _, w := range l.writers {
if level&w.Level() == 0 {
continue
}
w.Print(level, msg, args...)
}
}
// Printf logs messages with provided level in printf-style.
func (l *Logger) Printf(level int, format string, args ...interface{}) {
for _, w := range l.writers {
if level&w.Level() == 0 {
continue
}
w.Printf(level, format, args...)
}
}
// CheckErr checks error with default logger.
func CheckErr(err error, logFunc func(string, ...interface{})) (isErr bool) {
return logger.CheckErr(err, logFunc)
}
// CheckErr checks error, error will be logged if it's not equal to nil.
func (l *Logger) CheckErr(err error, logFunc func(string, ...interface{})) (isErr bool) {
for _, w := range l.writers {
if e := w.CheckErr(err, logFunc); e == true {
isErr = true
}
}
return
}
func checkErr(err error, logFunc func(string, ...interface{})) (isErr bool) {
if err != nil {
logFunc("error occurred", "error", err.Error())
}
return
}