-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleLog.go
More file actions
93 lines (83 loc) · 2.2 KB
/
handleLog.go
File metadata and controls
93 lines (83 loc) · 2.2 KB
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
package golog
// Log is a struct that represents
// a newly created log
//
// It is intended to be internal only,
// exported just to allow outside type
// reference
type Log struct {
lvl uint64
msg string
logger *logger
preFields LogFields
adHocFields []LogFields
postFields LogFields
}
// Field will return the value associated with the
// given key
//
// Note that if this method is called in pre hooks,
// the adHoc and post fields aren't ready yet, so the
// returned value may be overridden by them.
// If called in post hook N, it can only see the result
// of the application of the N-1 previous post hooks. The
// returned value can be overridden by the N+1 next post
// hooks
//
// Pay attention
func (l Log) Field(key string) interface{} {
if key == l.logger.configuration.MsgFieldName {
return l.msg
}
if key == l.logger.configuration.LvlFieldName {
return l.lvl
}
v := tryRead(key, l.postFields)
if v != nil {
return v
}
v = tryRead(key, l.adHocFields...)
if v != nil {
return v
}
v = tryRead(key, l.preFields)
if v != nil {
return v
}
return tryRead(key, l.logger.fields)
}
// -----
// handleLog will compile all the log fields and call
// the registered output functions.
//
// The src fields will be overridden by pre fields,
// that will be overridden by adHoc fields and latter by
// post fields
//
// Note that there are two reserved fields (lvl and msg),
// that will override any existing fields with the same
// configured keys
//
// Using var just to ease tests
var handleLog = func(log Log) {
defer func() {
if log.logger.configuration.AsyncScheduler == nil {
return
}
// Ignore any panics that may happen here, if it is async
recover()
}()
logFields := cloneOrNew(log.logger.fields)
mergeOverriding(logFields, log.preFields)
mergeOverriding(logFields, log.adHocFields...)
if len(log.logger.postHooks) != 0 {
log.postFields = LogFields{}
applyHooks(log, log.postFields, log.logger.postHooks)
mergeOverriding(logFields, log.postFields)
}
logFields[log.logger.configuration.LvlFieldName] = log.lvl
logFields[log.logger.configuration.MsgFieldName] = log.msg
for _, output := range log.logger.outputs {
output(log.lvl, log.msg, logFields)
}
}