-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.go
478 lines (418 loc) · 11.8 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
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
package log
import "os"
import "fmt"
import "time"
import "strings"
import stdlog "log"
import "github.com/prataprc/color"
var timeformat, prefix = "2006-01-02T15:04:05.999Z-07:00", "[%v]"
func init() {
setts := map[string]interface{}{
"log.level": "info",
"log.flags": "",
"log.file": "",
"log.timeformat": timeformat,
"log.prefix": prefix,
"log.colorignore": "",
"log.colorfatal": "red",
"log.colorerror": "hired",
"log.colorwarn": "yellow",
"log.colorinfo": "",
"log.colorverbose": "",
"log.colordebug": "",
"log.colortrace": "",
}
SetLogger(nil, setts)
}
var log Logger // can be used used off-the-shelf.
// DefaultLogLevel to use if log.level option is missing.
var DefaultLogLevel = "info"
// Logger interface for application logging, applications can
// supply a logger object implementing this interface, otherwise,
// defaultLogger{} will be used.
type Logger interface {
// SetLogLevel application's global log level, can be one of the
// following: "ignore", "fatal", "error", "warn", "info", "verbose",
// "debug", "trace"
SetLogLevel(string)
// SetLogFlags format of log prefix, following golang's log:Flag()
// specification
SetLogFlags(flags int)
// SetTimeFormat to use as prefix for all log messages.
SetTimeFormat(string)
// SetLogprefix including the log level.
SetLogprefix(interface{})
// SetLogcolor sets coloring attributes for specified log level, can be
// a list of following attributes: "bold", "underline", "blinkslow",
// "blinkrapid", "crossedout",
// "red", "green", "yellow", "blue", "magenta", "cyan", "white"
// "hired", "higreen", "hiyellow", "hiblue", "himagenta", "hicyan",
// "hiwhite"
SetLogcolor(level string, attrs []string)
// Fatalf similar to Printf, will be logged only when log level is set as
// "fatal" or above.
Fatalf(format string, v ...interface{})
// Errorf similar to Printf, will be logged only when log level is set as
// "error" or above.
Errorf(format string, v ...interface{})
// Warnf similar to Printf, will be logged only when log level is set as
// "warn" or above.
Warnf(format string, v ...interface{})
// Infof similar to Printf, will be logged only when log level is set as
// "info" or above.
Infof(format string, v ...interface{})
// Verbosef similar to Printf, will be logged only when log level is set as
// "verbose" or above.
Verbosef(format string, v ...interface{})
// Debugf similar to Printf, will be logged only when log level is set as
// "debug" or above.
Debugf(format string, v ...interface{})
// Tracef similar to Printf, will be logged only when log level is set as
// "trace" or above.
Tracef(format string, v ...interface{})
// Printlf reserved for future extension.
Printlf(loglevel LogLevel, format string, v ...interface{})
}
// LogLevel defines application log level.
type LogLevel int
const (
logLevelIgnore LogLevel = iota + 1
logLevelFatal
logLevelError
logLevelWarn
logLevelInfo
logLevelVerbose
logLevelDebug
logLevelTrace
)
// SetLogger to integrate storage logging with application logging.
// importing this package will initialize the logger with info level
// logging to console.
func SetLogger(logger Logger, setts map[string]interface{}) Logger {
if logger != nil {
log = logger
return log
}
var err error
logfd := os.Stdout
if logfile, ok := setts["log.file"]; ok {
filename := logfile.(string)
if filename != "" {
logfd, err = os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if err != nil {
if logfd, err = os.Create(filename); err != nil {
panic(err)
}
}
}
}
stdlog.SetOutput(logfd)
deflog := &defaultLogger{
timeformat: timeformat, prefix: prefix,
colors: make(map[LogLevel]*color.Color),
}
level, ok := setts["log.level"]
if ok == false {
level = "info"
}
deflog.SetLogLevel(level.(string))
logflags := int(0)
if flags, ok := setts["log.flags"]; ok {
for _, flag := range parsecsv(flags.(string)) {
logflags |= string2flag(flag)
}
deflog.SetLogFlags(logflags)
}
if logflags == 0 {
if timeformat, ok := setts["log.timeformat"]; ok {
deflog.SetTimeFormat(timeformat.(string))
}
} else { // if flags are available disable timeformat.
deflog.SetTimeFormat("")
}
if prefix, ok := setts["log.prefix"]; ok {
deflog.SetLogprefix(prefix)
}
// colors
params := []string{"log.colorignore", "log.colorfatal", "log.colorerror",
"log.colorwarn", "log.colorinfo", "log.colorverbose", "log.colordebug",
"log.colortrace"}
for _, param := range params {
level := param[9:]
if val, ok := setts[param]; ok {
if v1, ok := val.(string); ok {
deflog.SetLogcolor(level, parsecsv(v1))
} else if v2, ok := val.([]string); ok {
deflog.SetLogcolor(level, v2)
} else {
fmsg := "invalid type: color parameter %q has %T"
panic(fmt.Errorf(fmsg, param, val))
}
}
}
log = deflog
return log
}
// defaultLogger with default log-file as os.Stdout and,
// default log-level as logLevelInfo. Applications can
// supply a Logger{} object when instantiating the
// Transport.
type defaultLogger struct {
level LogLevel
timeformat string
prefix string
colors map[LogLevel]*color.Color
}
// SetLogLevel for defaultLogger.
func (l *defaultLogger) SetLogLevel(level string) {
l.level = string2logLevel(level)
}
// SetLogFlags for defaultLogger.
func (l *defaultLogger) SetLogFlags(flags int) {
stdlog.SetFlags(flags)
}
// SetTimeFormat for defaultLogger.
func (l *defaultLogger) SetTimeFormat(format string) {
l.timeformat = format
}
// SetLogprefix for defaultLogger
func (l *defaultLogger) SetLogprefix(prefix interface{}) {
if val, ok := prefix.(string); ok {
l.prefix = val
} else if _, ok = prefix.(bool); ok {
l.prefix = ""
} else {
panic("level-prefix can either be string format, or bool")
}
}
// SetLogcolor for defaultLogger
func (l *defaultLogger) SetLogcolor(level string, attrs []string) {
ll := string2logLevel(level)
attributes := []color.Attribute{}
for _, attr := range attrs {
attributes = append(attributes, string2clrattr(attr))
}
l.colors[ll] = color.New(attributes...)
}
// Fatalf for defaultLogger
func (l *defaultLogger) Fatalf(format string, v ...interface{}) {
l.Printlf(logLevelFatal, format, v...)
}
// Errorf for defaultLogger
func (l *defaultLogger) Errorf(format string, v ...interface{}) {
l.Printlf(logLevelError, format, v...)
}
// Warnf for defaultLogger
func (l *defaultLogger) Warnf(format string, v ...interface{}) {
l.Printlf(logLevelWarn, format, v...)
}
// Infof for defaultLogger
func (l *defaultLogger) Infof(format string, v ...interface{}) {
l.Printlf(logLevelInfo, format, v...)
}
// Verbosef for defaultLogger
func (l *defaultLogger) Verbosef(format string, v ...interface{}) {
l.Printlf(logLevelVerbose, format, v...)
}
// Debugf for defaultLogger
func (l *defaultLogger) Debugf(format string, v ...interface{}) {
l.Printlf(logLevelDebug, format, v...)
}
// Tracef for defaultLogger
func (l *defaultLogger) Tracef(format string, v ...interface{}) {
l.Printlf(logLevelTrace, format, v...)
}
// Printlf for defaultLogger
func (l *defaultLogger) Printlf(level LogLevel, frmt string, v ...interface{}) {
if l.canlog(level) {
prefix := ""
if l.timeformat != "" {
prefix = time.Now().Format(l.timeformat) + " "
}
if lstr := level.String(); lstr != "" && l.prefix != "" {
prefix += fmt.Sprintf(l.prefix, level.String()) + " "
}
newv := []interface{}{prefix}
newv = append(newv, v...)
frmt := trimformat(frmt) // output appends a newline because of color
if color, ok := l.colors[level]; ok && color != nil {
stdlog.Output(3, color.Sprintf("%v"+frmt, newv...))
} else {
stdlog.Output(3, fmt.Sprintf("%v"+frmt, newv...))
}
}
}
func (l *defaultLogger) canlog(level LogLevel) bool {
if level <= l.level {
return true
}
return false
}
func (l LogLevel) String() string {
switch l {
case logLevelIgnore:
return "Ignor"
case logLevelFatal:
return "Fatal"
case logLevelError:
return "Error"
case logLevelWarn:
return "Warng"
case logLevelInfo:
return "Infom"
case logLevelVerbose:
return "Verbs"
case logLevelDebug:
return "Debug"
case logLevelTrace:
return "Trace"
}
panic("unexpected log level") // should never reach here
}
func string2logLevel(s string) LogLevel {
s = strings.ToLower(s)
switch s {
case "ignore":
return logLevelIgnore
case "fatal":
return logLevelFatal
case "error":
return logLevelError
case "warn":
return logLevelWarn
case "info":
return logLevelInfo
case "verbose":
return logLevelVerbose
case "debug":
return logLevelDebug
case "trace":
return logLevelTrace
}
panic(fmt.Errorf("unexpected log level: %q", s)) // never reach here
}
func string2clrattr(s string) color.Attribute {
s = strings.ToLower(s)
switch s {
case "bold":
return color.Bold
case "underline":
return color.Underline
case "blinkslow":
return color.BlinkSlow
case "blinkrapid":
return color.BlinkRapid
case "crossedout":
return color.CrossedOut
case "red":
return color.FgRed
case "green":
return color.FgGreen
case "yellow":
return color.FgYellow
case "blue":
return color.FgBlue
case "magenta":
return color.FgMagenta
case "cyan":
return color.FgCyan
case "white":
return color.FgWhite
case "hired":
return color.FgHiRed
case "higreen":
return color.FgHiGreen
case "hiyellow":
return color.FgHiYellow
case "hiblue":
return color.FgHiBlue
case "himagenta":
return color.FgHiMagenta
case "hicyan":
return color.FgHiCyan
case "hiwhite":
return color.FgHiWhite
}
panic(fmt.Errorf("unexpected color attribute %q", s)) // never reach here
}
func string2flag(s string) int {
s = strings.ToLower(s)
switch s {
case "ldate":
return stdlog.Ldate
case "ltime":
return stdlog.Ltime
case "lmicroseconds":
return stdlog.Lmicroseconds
case "llongfile":
return stdlog.Llongfile
case "lshortfile":
return stdlog.Lshortfile
case "lutc":
return stdlog.LUTC
case "lstdflags":
return stdlog.LstdFlags
}
panic(fmt.Errorf("unexpected color attribute %q", s)) // never reach here
}
// Fatalf similar to Printf, will be logged only when log level is set as
// "fatal" or above.
func Fatalf(format string, v ...interface{}) {
log.Printlf(logLevelFatal, format, v...)
panic(fmt.Errorf(format, v...))
}
// Errorf similar to Printf, will be logged only when log level is set as
// "error" or above.
func Errorf(format string, v ...interface{}) {
log.Printlf(logLevelError, format, v...)
}
// Warnf similar to Printf, will be logged only when log level is set as
// "warn" or above.
func Warnf(format string, v ...interface{}) {
log.Printlf(logLevelWarn, format, v...)
}
// Infof similar to Printf, will be logged only when log level is set as
// "info" or above.
func Infof(format string, v ...interface{}) {
log.Printlf(logLevelInfo, format, v...)
}
// Verbosef similar to Printf, will be logged only when log level is set as
// "verbose" or above.
func Verbosef(format string, v ...interface{}) {
log.Printlf(logLevelVerbose, format, v...)
}
// Debugf similar to Printf, will be logged only when log level is set as
// "debug" or above.
func Debugf(format string, v ...interface{}) {
log.Printlf(logLevelDebug, format, v...)
}
// Tracef similar to Printf, will be logged only when log level is set as
// "trace" or above.
func Tracef(format string, v ...interface{}) {
log.Printlf(logLevelTrace, format, v...)
}
// Consolef similar to Printf, will log to os.Stdout.
func Consolef(format string, v ...interface{}) {
fmt.Fprintf(os.Stdout, format, v...)
}
func parsecsv(input string) []string {
if input == "" {
return nil
}
ss := strings.Split(input, ",")
outs := make([]string, 0)
for _, s := range ss {
s = strings.Trim(s, " \t\r\n")
if s == "" {
continue
}
outs = append(outs, s)
}
return outs
}
func trimformat(frmt string) string {
if frmt[len(frmt)-1] == '\n' {
return frmt[:len(frmt)-1]
}
return frmt
}