-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplexlog.go
550 lines (489 loc) · 12.6 KB
/
simplexlog.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Package simplexlog simple wrapper for the standard log package, that adds log level.
// simplexlog is concurrent safe.
package simplexlog
import (
"fmt"
"io"
"log"
"os"
"strings"
"sync"
)
const (
// DefaultLogFlags default flagsfor log
DefaultLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds
)
const (
// Log levels
// Critical log level
Critical LogLevel = iota
// Error log level
Error
// Warning log level
Warning
// Notice log level
Notice
// Info log level
Info
// Debug log level
Debug
// Trace log level
Trace
// All log level
All
// Level Name
// LevelCritical critical level
LevelCritical = "CRITICAL"
// LevelError error level
LevelError = "ERROR"
// LevelWarning warning level
LevelWarning = "WARNING"
// LevelNotice notice level
LevelNotice = "NOTICE"
// LevelInfo info level
LevelInfo = "INFO"
// LevelDebug debug level
LevelDebug = "DEBUG"
// LevelTrace trace level
LevelTrace = "TRACE"
// LevelAll all level
LevelAll = "ALL"
)
// LogLevel level of log
type LogLevel uint
// Logger simple log wrapper
type Logger struct {
logCritical,
logError,
logWarning,
logNotice,
logInfo,
logDebug,
logTrace *log.Logger
mutex sync.Mutex // guard the log level
level LogLevel
}
// Config log option
type Config struct {
// Out is the output writer
Out io.Writer
// Label the prefix of a log line
Label string
// Flags are the same combination of flag of standard log package
Flags int
}
// SetDefault set the options of default logger used by all log level except Error, Critical, Fatal and Panic
func SetDefault(c Config) func(*Logger) {
return func(l *Logger) {
l.logWarning = log.New(c.Out, c.Label, c.Flags)
l.logInfo = l.logWarning
l.logNotice = l.logWarning
l.logDebug = l.logWarning
l.logTrace = l.logWarning
}
}
// SetErrorDefault set the options of default logger for error (used by Error, Critical level and by Fatal and Panic)
func SetErrorDefault(c Config) func(*Logger) {
return func(l *Logger) {
l.logError = log.New(c.Out, c.Label, c.Flags)
l.logCritical = l.logError
}
}
// SetAllDefault set the options of default logger used by all the log level
func SetAllDefault(c Config) func(*Logger) {
return func(l *Logger) {
l.logWarning = log.New(c.Out, c.Label, c.Flags)
l.logInfo = l.logWarning
l.logNotice = l.logWarning
l.logDebug = l.logWarning
l.logTrace = l.logWarning
l.logError = l.logWarning
l.logCritical = l.logWarning
}
}
// SetOutput set only the output destination for a specified log level
func SetOutput(level LogLevel, w io.Writer) func(*Logger) {
return func(l *Logger) {
switch level {
case Info:
l.logInfo.SetOutput(w)
case Notice:
l.logNotice.SetOutput(w)
case Warning:
l.logWarning.SetOutput(w)
case Debug:
l.logDebug.SetOutput(w)
case Trace:
l.logTrace.SetOutput(w)
case Error:
l.logError.SetOutput(w)
case Critical:
l.logCritical.SetOutput(w)
case All:
l.logInfo.SetOutput(w)
l.logNotice.SetOutput(w)
l.logWarning.SetOutput(w)
l.logDebug.SetOutput(w)
l.logTrace.SetOutput(w)
l.logError.SetOutput(w)
l.logCritical.SetOutput(w)
}
}
}
// SetDebug set the options of debug logger
func SetDebug(c Config) func(*Logger) {
return func(l *Logger) {
l.logDebug = log.New(c.Out, c.Label, c.Flags)
}
}
// SetTrace set the options of trace logger
func SetTrace(c Config) func(*Logger) {
return func(l *Logger) {
l.logTrace = log.New(c.Out, c.Label, c.Flags)
}
}
// SetCritical set the options of critical logger
func SetCritical(c Config) func(*Logger) {
return func(l *Logger) {
l.logCritical = log.New(c.Out, c.Label, c.Flags)
}
}
// SetError set the options of error logger
func SetError(c Config) func(*Logger) {
return func(l *Logger) {
l.logError = log.New(c.Out, c.Label, c.Flags)
}
}
// SetWarning set the option of warning logger
func SetWarning(o Config) func(*Logger) {
return func(l *Logger) {
l.logWarning = log.New(o.Out, o.Label, o.Flags)
}
}
// SetNotice set the option of notice logger
func SetNotice(c Config) func(*Logger) {
return func(l *Logger) {
l.logNotice = log.New(c.Out, c.Label, c.Flags)
}
}
// SetInfo set the option of info logger
func SetInfo(c Config) func(*Logger) {
return func(l *Logger) {
l.logInfo = log.New(c.Out, c.Label, c.Flags)
}
}
// New return a new logger. By default, all logs message are output to os.Stdout, except "error" and "critical" message that are logged to os.Stderr.
func New(configurations ...func(*Logger)) *Logger {
// default log config
logger := Logger{
logCritical: log.New(os.Stderr, fmt.Sprintf("%-9s", LevelCritical), DefaultLogFlags),
logError: log.New(os.Stderr, fmt.Sprintf("%-9s", LevelError), DefaultLogFlags),
logWarning: log.New(os.Stdout, fmt.Sprintf("%-9s", LevelWarning), DefaultLogFlags),
logNotice: log.New(os.Stdout, fmt.Sprintf("%-9s", LevelNotice), DefaultLogFlags),
logInfo: log.New(os.Stdout, fmt.Sprintf("%-9s", LevelInfo), DefaultLogFlags),
logDebug: log.New(os.Stdout, fmt.Sprintf("%-9s", LevelDebug), DefaultLogFlags),
logTrace: log.New(os.Stdout, fmt.Sprintf("%-9s", LevelTrace), DefaultLogFlags),
level: Info,
}
// now customize logger
for _, config := range configurations {
config(&logger)
}
return &logger
}
// SwitchTo change the log level, level can be of type string (must match, case insensitive, level name like LevelTrace, LevelCritical etc), int or LogLevel to take effect
func (l *Logger) SwitchTo(level interface{}) {
switch lvl := level.(type) {
case string:
l.switchToLevel(lvl)
case int, LogLevel:
l.switchTo(lvl.(LogLevel))
}
}
// switchTo change the log level
func (l *Logger) switchTo(level LogLevel) {
if level < Critical || level > All {
return
}
l.mutex.Lock()
l.level = level
l.mutex.Unlock()
}
// switchToLevel change log level, must match (case insensitive) level name (like LevelTrace, LevelCritical etc)
func (l *Logger) switchToLevel(level string) {
level = strings.TrimSpace(strings.ToUpper(level))
l.mutex.Lock()
defer l.mutex.Unlock()
switch level {
case LevelCritical:
l.level = Critical
case LevelError:
l.level = Error
case LevelWarning:
l.level = Warning
case LevelNotice:
l.level = Notice
case LevelInfo:
l.level = Info
case LevelDebug:
l.level = Debug
case LevelTrace:
l.level = Trace
case LevelAll:
l.level = All
}
}
// SetOutput set the output destination for a specified log level
func (l *Logger) SetOutput(level LogLevel, w io.Writer) {
switch level {
case Info:
l.logInfo.SetOutput(w)
case Notice:
l.logNotice.SetOutput(w)
case Warning:
l.logWarning.SetOutput(w)
case Debug:
l.logDebug.SetOutput(w)
case Trace:
l.logTrace.SetOutput(w)
case Error:
l.logError.SetOutput(w)
case Critical:
l.logCritical.SetOutput(w)
case All:
l.logInfo.SetOutput(w)
l.logNotice.SetOutput(w)
l.logWarning.SetOutput(w)
l.logDebug.SetOutput(w)
l.logTrace.SetOutput(w)
l.logError.SetOutput(w)
l.logCritical.SetOutput(w)
}
}
// SetLabel set the label for a specified log level
func (l *Logger) SetLabel(level LogLevel, label string) {
switch level {
case Info:
l.logInfo.SetPrefix(label)
case Notice:
l.logNotice.SetPrefix(label)
case Warning:
l.logWarning.SetPrefix(label)
case Debug:
l.logDebug.SetPrefix(label)
case Trace:
l.logTrace.SetPrefix(label)
case Error:
l.logError.SetPrefix(label)
case Critical:
l.logCritical.SetPrefix(label)
case All:
l.logInfo.SetPrefix(label)
l.logNotice.SetPrefix(label)
l.logWarning.SetPrefix(label)
l.logDebug.SetPrefix(label)
l.logTrace.SetPrefix(label)
l.logError.SetPrefix(label)
l.logCritical.SetPrefix(label)
}
}
// SetFlags set the flags for a specified log level
func (l *Logger) SetFlags(level LogLevel, flag int) {
switch level {
case Info:
l.logInfo.SetFlags(flag)
case Notice:
l.logNotice.SetFlags(flag)
case Warning:
l.logWarning.SetFlags(flag)
case Debug:
l.logDebug.SetFlags(flag)
case Trace:
l.logTrace.SetFlags(flag)
case Error:
l.logError.SetFlags(flag)
case Critical:
l.logCritical.SetFlags(flag)
case All:
l.logInfo.SetFlags(flag)
l.logNotice.SetFlags(flag)
l.logWarning.SetFlags(flag)
l.logDebug.SetFlags(flag)
l.logTrace.SetFlags(flag)
l.logError.SetFlags(flag)
l.logCritical.SetFlags(flag)
}
}
// Level return the current log level
func (l *Logger) Level() LogLevel {
l.mutex.Lock()
defer l.mutex.Unlock()
return l.level
}
// LevelName return the current level name
func (l *Logger) LevelName() string {
l.mutex.Lock()
defer l.mutex.Unlock()
switch l.level {
case Critical:
return LevelCritical
case Error:
return LevelError
case Warning:
return LevelWarning
case Notice:
return LevelNotice
case Info:
return LevelInfo
case Debug:
return LevelDebug
case Trace:
return LevelTrace
case All:
return LevelAll
default:
return "?"
}
}
// LevelNames return all the availables level name, from the most specific to the least specific (all logs)
func (l *Logger) LevelNames() string {
return strings.Join(
[]string{
LevelCritical,
LevelError,
LevelWarning,
LevelNotice,
LevelInfo,
LevelDebug,
LevelTrace,
LevelAll},
", ")
}
// Infof print, according to format, to the Info logger
func (l *Logger) Infof(format string, v ...interface{}) {
if l.Level() >= Info {
l.logInfo.Printf(format, v...)
}
}
// Noticef print, according to format, to the Notice logger
func (l *Logger) Noticef(format string, v ...interface{}) {
if l.Level() >= Notice {
l.logNotice.Printf(format, v...)
}
}
// Warningf print, according to format, to the Warning logger
func (l *Logger) Warningf(format string, v ...interface{}) {
if l.Level() >= Warning {
l.logWarning.Printf(format, v...)
}
}
// Errorf print, according to format, to the Error logger
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.Level() >= Error {
l.logError.Printf(format, v...)
}
}
// Criticalf print, according to format, to the Critical logger
func (l *Logger) Criticalf(format string, v ...interface{}) {
if l.Level() >= Critical {
l.logCritical.Printf(format, v...)
}
}
// Debugf print, according to format, to the Debug logger
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.Level() >= Debug {
l.logDebug.Printf(format, v...)
}
}
// Tracef print, according to format, to the Debug logger
func (l *Logger) Tracef(format string, v ...interface{}) {
if l.Level() >= Trace {
l.logTrace.Printf(format, v...)
}
}
// Fatalf like log.Fatalf print to the Critical logger, according to format, followed by call to os.Exit(1)
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.logCritical.Fatalf(format, v...)
}
// Panicf like log.Panicf print to the Critical logger, according to format, followed by call to panic()
func (l *Logger) Panicf(format string, v ...interface{}) {
l.logCritical.Panicf(format, v...)
}
// Info print to the Info logger
func (l *Logger) Info(a ...interface{}) {
if l.Level() >= Info {
l.logInfo.Print(a...)
}
}
// Notice print to the Notice logger
func (l *Logger) Notice(a ...interface{}) {
if l.Level() >= Notice {
l.logNotice.Print(a...)
}
}
// Warning print to the Warning logger
func (l *Logger) Warning(a ...interface{}) {
if l.Level() >= Warning {
l.logWarning.Print(a...)
}
}
// Error print to the Error logger
func (l *Logger) Error(a ...interface{}) {
if l.Level() >= Error {
l.logError.Print(a...)
}
}
// Critical print to the Critical logger
func (l *Logger) Critical(a ...interface{}) {
if l.Level() >= Critical {
l.logCritical.Print(a...)
}
}
// Debug print to the Debug logger
func (l *Logger) Debug(a ...interface{}) {
if l.Level() >= Debug {
l.logDebug.Print(a...)
}
}
// Trace print to the Debug logger
func (l *Logger) Trace(a ...interface{}) {
if l.Level() >= Trace {
l.logTrace.Print(a...)
}
}
// Fatal like log.Fatal, print to the Critical logger, followed by call to os.Exit(1)
func (l *Logger) Fatal(a ...interface{}) {
l.logCritical.Fatal(a...)
}
// Panic like log.Panic, print to the Critical logger, followed by call to panic()
func (l *Logger) Panic(a ...interface{}) {
l.logCritical.Panic(a...)
}
// InfoLogger return the info logger
func (l *Logger) InfoLogger() *log.Logger {
return l.logInfo
}
// NoticeLogger return the error logger
func (l *Logger) NoticeLogger() *log.Logger {
return l.logCritical
}
// WarningLogger return the warning logger
func (l *Logger) WarningLogger() *log.Logger {
return l.logWarning
}
// ErrorLogger Return the error logger
func (l *Logger) ErrorLogger() *log.Logger {
return l.logError
}
// CriticalLogger return the error logger
func (l *Logger) CriticalLogger() *log.Logger {
return l.logCritical
}
// DebugLogger return the debug logger
func (l *Logger) DebugLogger() *log.Logger {
return l.logDebug
}
// TraceLogger return the trace logger
func (l *Logger) TraceLogger() *log.Logger {
return l.logTrace
}