forked from teambition/gear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
600 lines (530 loc) · 15.5 KB
/
logger.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package logging
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/teambition/gear"
)
var crlfEscaper = strings.NewReplacer("\r", "\\r", "\n", "\\n")
// Messager is implemented by any value that has a Format method and a String method.
// They are using by Logger to format value to string.
type Messager interface {
fmt.Stringer
Format() (string, error)
}
// Log records key-value pairs for structured logging.
type Log map[string]interface{}
// Format try to marshal the structured log with json.Marshal.
func (l Log) Format() (string, error) {
res, err := json.Marshal(l)
if err == nil {
return string(res), nil
}
return "", err
}
// GoString implemented fmt.GoStringer interface, returns a Go-syntax string.
func (l Log) GoString() string {
count := len(l)
buf := bytes.NewBufferString("Log{")
for key, value := range l {
if count--; count == 0 {
fmt.Fprintf(buf, "%s:%#v}", key, value)
} else {
fmt.Fprintf(buf, "%s:%#v, ", key, value)
}
}
return buf.String()
}
// String implemented fmt.Stringer interface, returns a Go-syntax string.
func (l Log) String() string {
return l.GoString()
}
// From copy values from the Log argument, returns self.
// log := Log{"key": "foo"}
// logging.Info(log.From(Log{"key2": "foo2"}))
func (l Log) From(log Log) Log {
for key, val := range log {
l[key] = val
}
return l
}
// Into copy self values into the Log argument, returns the Log argument.
// redisLog := Log{"kind": "redis"}
// logging.Err(redisLog.Into(Log{"data": "foo"}))
func (l Log) Into(log Log) Log {
for key, val := range l {
log[key] = val
}
return log
}
// With copy values from the argument, returns new log.
// log := Log{"key": "foo"}
// logging.Info(log.With(Log{"key2": "foo2"}))
func (l Log) With(log map[string]interface{}) Log {
cp := l.Into(Log{})
for key, val := range log {
cp[key] = val
}
return cp
}
// Reset delete all key-value on the log. Empty log will not be consumed.
//
// log := logger.FromCtx(ctx)
// if ctx.Path == "/" {
// log.Reset() // reset log, don't logging for path "/"
// } else {
// log["Data"] = someData
// }
//
func (l Log) Reset() {
for key := range l {
delete(l, key)
}
}
// Level represents logging level
// https://tools.ietf.org/html/rfc5424
// https://en.wikipedia.org/wiki/Syslog
type Level uint8
const (
// EmergLevel is 0, "Emergency", system is unusable
EmergLevel Level = iota
// AlertLevel is 1, "Alert", action must be taken immediately
AlertLevel
// CritiLevel is 2, "Critical", critical conditions
CritiLevel
// ErrLevel is 3, "Error", error conditions
ErrLevel
// WarningLevel is 4, "Warning", warning conditions
WarningLevel
// NoticeLevel is 5, "Notice", normal but significant condition
NoticeLevel
// InfoLevel is 6, "Informational", informational messages
InfoLevel
// DebugLevel is 7, "Debug", debug-level messages
DebugLevel
)
func (l Level) String() string {
switch l {
case EmergLevel:
return "EMERG"
case AlertLevel:
return "ALERT"
case CritiLevel:
return "CRIT"
case ErrLevel:
return "ERR"
case WarningLevel:
return "WARNING"
case NoticeLevel:
return "NOTICE"
case InfoLevel:
return "INFO"
case DebugLevel:
return "DEBUG"
default:
return "LOG"
}
}
var std = New(os.Stderr)
// Default returns the default logger
// If devMode is true, logger will print a simple version of Common Log Format with terminal color
func Default(devMode ...bool) *Logger {
if len(devMode) > 0 && devMode[0] {
std.SetLogConsume(developmentConsume)
}
return std
}
// a simple version of Common Log Format with terminal color
// https://en.wikipedia.org/wiki/Common_Log_Format
//
// 127.0.0.1 - - [2017-06-01T12:23:13.161Z] "GET /context.go?query=xxx HTTP/1.1" 200 21559 5.228ms
//
func developmentConsume(log Log, ctx *gear.Context) {
std.mu.Lock() // don't need Lock usually, logger.Output do it for us.
defer std.mu.Unlock()
end := time.Now().UTC()
FprintWithColor(std.Out, fmt.Sprintf("%s", log["IP"]), ColorGreen)
fmt.Fprintf(std.Out, ` - - [%s] "%s %s %s" `, end.Format(std.tf), log["Method"], log["URL"], log["Proto"])
status := log["Status"].(int)
FprintWithColor(std.Out, strconv.Itoa(status), colorStatus(status))
resTime := float64(end.Sub(log["Start"].(time.Time))) / 1e6
fmt.Fprintln(std.Out, fmt.Sprintf(" %d %.3fms", log["Length"], resTime))
}
// New creates a Logger instance with given io.Writer and DebugLevel log level.
// the logger timestamp format is "2006-01-02T15:04:05.999Z"(JavaScript ISO date string), log format is "[%s] %s %s"
func New(w io.Writer) *Logger {
logger := &Logger{Out: w}
logger.SetLevel(DebugLevel)
logger.SetTimeFormat("2006-01-02T15:04:05.999Z")
logger.SetLogFormat("[%s] %s %s")
logger.init = func(log Log, ctx *gear.Context) {
log["IP"] = ctx.IP().String()
log["Method"] = ctx.Method
log["URL"] = ctx.Req.URL.String()
log["Proto"] = ctx.Req.Proto
log["UserAgent"] = ctx.GetHeader(gear.HeaderUserAgent)
log["Start"] = time.Now()
}
logger.consume = func(log Log, _ *gear.Context) {
end := time.Now()
if t, ok := log["Start"].(time.Time); ok {
log["Time"] = end.Sub(t) / 1e6 // ms
}
if str, err := log.Format(); err == nil {
logger.Output(end, InfoLevel, str)
} else {
logger.Output(end, WarningLevel, log.String())
}
}
return logger
}
// A Logger represents an active logging object that generates lines of
// output to an io.Writer. Each logging operation makes a single call to
// the Writer's Write method. A Logger can be used simultaneously from
// multiple goroutines; it guarantees to serialize access to the Writer.
//
// A custom logger example:
//
// app := gear.New()
//
// logger := logging.New(os.Stdout)
// logger.SetLevel(logging.InfoLevel)
// logger.SetLogInit(func(log logging.Log, ctx *gear.Context) {
// log["IP"] = ctx.IP()
// log["Method"] = ctx.Method
// log["URL"] = ctx.Req.URL.String()
// log["Start"] = time.Now()
// log["UserAgent"] = ctx.GetHeader(gear.HeaderUserAgent)
// })
// logger.SetLogConsume(func(log logging.Log, _ *gear.Context) {
// end := time.Now()
// if str, err := log.Format(); err == nil {
// logger.Output(end, logging.InfoLevel, str)
// } else {
// logger.Output(end, logging.WarningLevel, log.String())
// }
// })
//
// app.UseHandler(logger)
// app.Use(func(ctx *gear.Context) error {
// log := logger.FromCtx(ctx)
// log["Data"] = []int{1, 2, 3}
// return ctx.HTML(200, "OK")
// })
//
type Logger struct {
// Destination for output, It's common to set this to a
// file, or `os.Stderr`. You can also set this to
// something more adventorous, such as logging to Kafka.
Out io.Writer
l Level // logging level
tf, lf string // time format, log format
mu sync.Mutex // ensures atomic writes; protects the following fields
init func(Log, *gear.Context) // hook to initialize log with gear.Context
consume func(Log, *gear.Context) // hook to consume log
}
// Check log output level statisfy output level or not, used internal, for performance
func (l *Logger) checkLogLevel(level Level) bool {
// don't satisfy logger level, so skip
return level <= l.l
}
// Emerg produce a "Emergency" log
func (l *Logger) Emerg(v interface{}) {
l.Output(time.Now(), EmergLevel, formatError(v))
}
// Alert produce a "Alert" log
func (l *Logger) Alert(v interface{}) {
if l.checkLogLevel(AlertLevel) {
l.Output(time.Now(), AlertLevel, formatError(v))
}
}
// Crit produce a "Critical" log
func (l *Logger) Crit(v interface{}) {
if l.checkLogLevel(CritiLevel) {
l.Output(time.Now(), CritiLevel, formatError(v))
}
}
// Err produce a "Error" log
func (l *Logger) Err(v interface{}) {
if l.checkLogLevel(ErrLevel) {
l.Output(time.Now(), ErrLevel, formatError(v))
}
}
// Warning produce a "Warning" log
func (l *Logger) Warning(v interface{}) {
if l.checkLogLevel(WarningLevel) {
l.Output(time.Now(), WarningLevel, format(v))
}
}
// Notice produce a "Notice" log
func (l *Logger) Notice(v interface{}) {
if l.checkLogLevel(NoticeLevel) {
l.Output(time.Now(), NoticeLevel, format(v))
}
}
// Info produce a "Informational" log
func (l *Logger) Info(v interface{}) {
if l.checkLogLevel(InfoLevel) {
l.Output(time.Now(), InfoLevel, format(v))
}
}
// Debug produce a "Debug" log
func (l *Logger) Debug(v interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, format(v))
}
}
// Debugf produce a "Debug" log in the manner of fmt.Printf
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, fmt.Sprintf(format, args...))
}
}
// Panic produce a "Emergency" log and then calls panic with the message
func (l *Logger) Panic(v interface{}) {
s := format(v)
l.Emerg(s)
panic(s)
}
var exit = func() { os.Exit(1) }
// Fatal produce a "Emergency" log and then calls os.Exit(1)
func (l *Logger) Fatal(v interface{}) {
l.Emerg(v)
exit()
}
// Print produce a log in the manner of fmt.Print, without timestamp and log level
func (l *Logger) Print(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.Out, args...)
}
// Printf produce a log in the manner of fmt.Printf, without timestamp and log level
func (l *Logger) Printf(format string, args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintf(l.Out, format, args...)
}
// Println produce a log in the manner of fmt.Println, without timestamp and log level
func (l *Logger) Println(args ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprintln(l.Out, args...)
}
// Output writes a string log with timestamp and log level to the output.
// If the level is greater than logger level, the log will be omitted.
// The log will be format by timeFormat and logFormat.
func (l *Logger) Output(t time.Time, level Level, s string) (err error) {
l.mu.Lock()
defer l.mu.Unlock()
if l := len(s); l > 0 && s[l-1] == '\n' {
s = s[0 : l-1]
}
_, err = fmt.Fprintf(l.Out, l.lf, t.UTC().Format(l.tf), level.String(), crlfEscaper.Replace(s))
if err == nil {
l.Out.Write([]byte{'\n'})
}
return
}
// SetLevel set the logger's log level
// The default logger level is DebugLevel
func (l *Logger) SetLevel(level Level) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
if level > DebugLevel {
panic(gear.Err.WithMsg("invalid logger level"))
}
l.l = level
return l
}
// SetTimeFormat set the logger timestamp format
// The default logger timestamp format is "2006-01-02T15:04:05.999Z"(JavaScript ISO date string)
func (l *Logger) SetTimeFormat(timeFormat string) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.tf = timeFormat
return l
}
// SetLogFormat set the logger log format
// it should accept 3 string values: timestamp, log level and log message
// The default logger log format is "[%s] %s %s": "[time] logLevel message"
func (l *Logger) SetLogFormat(logFormat string) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.lf = logFormat
return l
}
// SetLogInit set a log init handle to the logger.
// It will be called when log created.
func (l *Logger) SetLogInit(fn func(Log, *gear.Context)) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.init = fn
return l
}
// SetLogConsume set a log consumer handle to the logger.
// It will be called on a "end hook" and should write the log to underlayer logging system.
// The default implements is for development, the output log format:
//
// 127.0.0.1 GET /text 200 6500 - 0.765 ms
//
// Please implements a Log Consume for your production.
func (l *Logger) SetLogConsume(fn func(Log, *gear.Context)) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.consume = fn
return l
}
// New implements gear.Any interface,then we can use ctx.Any to retrieve a Log instance from ctx.
// Here also some initialization work after created.
func (l *Logger) New(ctx *gear.Context) (interface{}, error) {
log := Log{}
l.init(log, ctx)
return log, nil
}
// FromCtx retrieve the Log instance from the ctx with ctx.Any.
// Logger.New and ctx.Any will guarantee it exists.
func (l *Logger) FromCtx(ctx *gear.Context) Log {
any, _ := ctx.Any(l)
return any.(Log)
}
// SetTo sets key/value to the Log instance on ctx.
// app.Use(func(ctx *gear.Context) error {
// logging.SetTo(ctx, "Data", []int{1, 2, 3})
// return ctx.HTML(200, "OK")
// })
func (l *Logger) SetTo(ctx *gear.Context, key string, val interface{}) {
any, _ := ctx.Any(l)
any.(Log)[key] = val
}
// Serve implements gear.Handler interface, we can use logger as gear middleware.
//
// app := gear.New()
// app.UseHandler(logging.Default())
// app.Use(func(ctx *gear.Context) error {
// log := logging.FromCtx(ctx)
// log["Data"] = []int{1, 2, 3}
// return ctx.HTML(200, "OK")
// })
//
func (l *Logger) Serve(ctx *gear.Context) error {
log := l.FromCtx(ctx)
// Add a "end hook" to flush logs
ctx.OnEnd(func() {
// Ignore empty log
if len(log) == 0 {
return
}
log["Status"] = ctx.Res.Status()
log["Length"] = len(ctx.Res.Body())
l.consume(log, ctx)
})
return nil
}
// Emerg produce a "Emergency" log with the default logger
func Emerg(v interface{}) {
std.Emerg(v)
}
// Alert produce a "Alert" log with the default logger
func Alert(v interface{}) {
std.Alert(v)
}
// Crit produce a "Critical" log with the default logger
func Crit(v interface{}) {
std.Crit(v)
}
// Err produce a "Error" log with the default logger
func Err(v interface{}) {
std.Err(v)
}
// Warning produce a "Warning" log with the default logger
func Warning(v interface{}) {
std.Warning(v)
}
// Notice produce a "Notice" log with the default logger
func Notice(v interface{}) {
std.Notice(v)
}
// Info produce a "Informational" log with the default logger
func Info(v interface{}) {
std.Info(v)
}
// Debug produce a "Debug" log with the default logger
func Debug(v interface{}) {
std.Debug(v)
}
// Debugf produce a "Debug" log in the manner of fmt.Printf with the default logger
func Debugf(format string, args ...interface{}) {
std.Debugf(format, args...)
}
// Panic produce a "Emergency" log with the default logger and then calls panic with the message
func Panic(v interface{}) {
std.Panic(v)
}
// Fatal produce a "Emergency" log with the default logger and then calls os.Exit(1)
func Fatal(v interface{}) {
std.Fatal(v)
}
// Print produce a log in the manner of fmt.Print with the default logger,
// without timestamp and log level
func Print(args ...interface{}) {
std.Print(args...)
}
// Printf produce a log in the manner of fmt.Printf with the default logger,
// without timestamp and log level
func Printf(format string, args ...interface{}) {
std.Printf(format, args...)
}
// Println produce a log in the manner of fmt.Println with the default logger,
// without timestamp and log level
func Println(args ...interface{}) {
std.Println(args...)
}
// FromCtx retrieve the Log instance for the default logger.
func FromCtx(ctx *gear.Context) Log {
return std.FromCtx(ctx)
}
// SetTo sets key/value to the Log instance on ctx for the default logger.
// app.UseHandler(logging.Default())
// app.Use(func(ctx *gear.Context) error {
// logging.SetTo(ctx, "Data", []int{1, 2, 3})
// return ctx.HTML(200, "OK")
// })
func SetTo(ctx *gear.Context, key string, val interface{}) {
std.SetTo(ctx, key, val)
}
func colorStatus(code int) ColorType {
switch {
case code < 300:
return ColorGreen
case code >= 300 && code < 400:
return ColorCyan
case code >= 400 && code < 500:
return ColorYellow
default:
return ColorRed
}
}
func formatError(i interface{}) string {
err := gear.ErrorWithStack(i, 3)
if str, e := err.Format(); e == nil {
return str
}
return err.String()
}
func format(i interface{}) string {
switch v := i.(type) {
case Messager:
if str, err := v.Format(); err == nil {
return str
}
return v.String()
default:
return fmt.Sprint(i)
}
}