-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
77 lines (68 loc) · 2.02 KB
/
context.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
package log
import (
"bytes"
)
// C creates a logging context
func C(tag string, fields ...interface{}) *Context {
var buf bytes.Buffer
writeFields(&buf, fields...)
emitter := &Emitter{
Level: Default.Level,
Output: Default.Output,
TimeFormat: Default.TimeFormat,
Hook: Default.Hook,
context: buf.Bytes(),
}
return &Context{Emitter: emitter, Tag: tag}
}
// Context is a logging emitter wrapper with parsed context fields
//
// The common use case is
//
// logger := log.C("TAG", "one", 1, "two", 2)
// logger.I("start")
// logger.I("stop")
//
// The output is something like below
//
// {"tag":"TAG","msg":"start","one":1,"two":2}
// {"tag":"TAG","msg":"stop","one":1,"two":2}
type Context struct {
Emitter *Emitter
Tag string
}
// C returns a new context based on the current context
func (c *Context) C(fields ...interface{}) *Context {
var buf bytes.Buffer
buf.Write(c.Emitter.context)
writeFields(&buf, fields...)
emitter := &Emitter{
Level: c.Emitter.Level,
Output: c.Emitter.Output,
TimeFormat: c.Emitter.TimeFormat,
Hook: c.Emitter.Hook,
context: buf.Bytes(),
}
return &Context{Emitter: emitter, Tag: c.Tag}
}
// T logs a message when the Level is set to Trace
func (c *Context) T(message string, fields ...interface{}) {
c.Emitter.Emit(c.Tag, Trace, message, fields...)
}
// I logs a message when the Level is set to Info or lower
func (c *Context) I(message string, fields ...interface{}) {
c.Emitter.Emit(c.Tag, Info, message, fields...)
}
// W logs a message when the Level is set to Warn or lower
func (c *Context) W(message string, fields ...interface{}) {
c.Emitter.Emit(c.Tag, Warn, message, fields...)
}
// E logs a message when the Level is set to Error or lower
func (c *Context) E(message string, fields ...interface{}) {
c.Emitter.Emit(c.Tag, Error, message, fields...)
}
// F logs a message when the Level is set to Fatal or lower
func (c *Context) F(message string, fields ...interface{}) {
c.Emitter.Emit(c.Tag, Fatal, message, fields...)
_exit(1)
}