forked from teambition/gear
/
logger.go
455 lines (400 loc) · 12 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
package logging
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"os"
"strconv"
"sync"
"time"
"github.com/eavesmy/gear"
)
// Log records key-value pairs for structured logging.
type Log map[string]interface{}
// JSON try to marshal the structured log with json.Marshal.
func (l Log) JSON() (string, error) {
res, err := json.Marshal(l)
if err == nil {
return string(res), nil
}
return "", err
}
// String implemented fmt.Stringer interface, returns a Go-syntax string.
func (l Log) String() 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()
}
// 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
)
var levels = []string{"EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG"}
var std = New(os.Stderr)
// Default returns the default logger
func Default() *Logger {
return std
}
// 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()
log["Method"] = ctx.Method
log["URL"] = ctx.Req.URL.String()
log["Start"] = time.Now()
}
logger.consume = func(log Log, ctx *gear.Context) {
logger.mu.Lock() // don't need Lock usually, logger.Output do it for us.
defer logger.mu.Unlock()
fmt.Fprintf(logger.Out, "%s %s %s ", log["IP"].(net.IP), log["Method"].(string), log["URL"].(string))
status := log["Status"].(int)
FprintWithColor(logger.Out, strconv.Itoa(status), colorStatus(status))
fmt.Fprintln(logger.Out, fmt.Sprintf(
" %s - %.3f ms", log["Length"], float64(time.Now().Sub(log["Start"].(time.Time)))/1e6))
}
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 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.Get(gear.HeaderUserAgent)
// })
// logger.SetLogConsume(func(log Log, _ *gear.Context) {
// end := time.Now()
// log["Time"] = end.Sub(log["Start"].(time.Time)) / 1e6
// delete(log, "Start")
// if res, err := log.JSON(); err == nil {
// logger.Output(end, InfoLevel, res)
// } else {
// logger.Output(end, 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, fmt.Sprint(v))
}
// Alert produce a "Alert" log
func (l *Logger) Alert(v interface{}) {
if l.checkLogLevel(AlertLevel) {
l.Output(time.Now(), AlertLevel, fmt.Sprint(v))
}
}
// Crit produce a "Critical" log
func (l *Logger) Crit(v interface{}) {
if l.checkLogLevel(CritiLevel) {
l.Output(time.Now(), CritiLevel, fmt.Sprint(v))
}
}
// Err produce a "Error" log
func (l *Logger) Err(v interface{}) {
if l.checkLogLevel(ErrLevel) {
l.Output(time.Now(), ErrLevel, fmt.Sprint(v))
}
}
// Warning produce a "Warning" log
func (l *Logger) Warning(v interface{}) {
if l.checkLogLevel(WarningLevel) {
l.Output(time.Now(), WarningLevel, fmt.Sprint(v))
}
}
// Notice produce a "Notice" log
func (l *Logger) Notice(v interface{}) {
if l.checkLogLevel(NoticeLevel) {
l.Output(time.Now(), NoticeLevel, fmt.Sprint(v))
}
}
// Info produce a "Informational" log
func (l *Logger) Info(v interface{}) {
if l.checkLogLevel(InfoLevel) {
l.Output(time.Now(), InfoLevel, fmt.Sprint(v))
}
}
// Debug produce a "Debug" log
func (l *Logger) Debug(v interface{}) {
if l.checkLogLevel(DebugLevel) {
l.Output(time.Now(), DebugLevel, fmt.Sprint(v))
}
}
// Panic produce a "Emergency" log and then calls panic with the message
func (l *Logger) Panic(v interface{}) {
s := fmt.Sprint(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()
// log level checked before
if level < 4 {
s = gear.ErrorWithStack(s, 4).String()
}
_, err = fmt.Fprintf(l.Out, l.lf, t.UTC().Format(l.tf), levels[level], s)
if err == nil && s[len(s)-1] != '\n' {
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) {
l.mu.Lock()
defer l.mu.Unlock()
if level > DebugLevel {
panic(gear.NewAppError("invalid logger level"))
}
l.l = level
}
// 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) {
l.mu.Lock()
defer l.mu.Unlock()
l.tf = timeFormat
}
// 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"
func (l *Logger) SetLogFormat(logFormat string) {
l.mu.Lock()
defer l.mu.Unlock()
l.lf = logFormat
}
// 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)) {
l.mu.Lock()
defer l.mu.Unlock()
l.init = fn
}
// 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 WriteLog for your production.
func (l *Logger) SetLogConsume(fn func(Log, *gear.Context)) {
l.mu.Lock()
defer l.mu.Unlock()
l.consume = fn
}
// 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)
}
// 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.Status()
log["Type"] = ctx.Res.Get(gear.HeaderContentType)
log["Length"] = ctx.Res.Get(gear.HeaderContentLength)
// Don't block current process.
go 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)
}
// 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)
}
func colorStatus(code int) ColorType {
switch {
case code >= 200 && code < 300:
return ColorGreen
case code >= 300 && code < 400:
return ColorCyan
case code >= 400 && code < 500:
return ColorYellow
default:
return ColorRed
}
}