/
logLevels.go
149 lines (129 loc) · 4.88 KB
/
logLevels.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
package logger
// Trace will create a new log with the Trace level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Trace(msg string, adHocFields ...LogFields) { l.Log(LvlTrace, msg, adHocFields) }
// Debug will create a new log with the Debug level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Debug(msg string, adHocFields ...LogFields) { l.Log(LvlDebug, msg, adHocFields) }
// Info will create a new log with the Info level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Info(msg string, adHocFields ...LogFields) { l.Log(LvlInfo, msg, adHocFields) }
// Warn will create a new log with the Warn level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Warn(msg string, adHocFields ...LogFields) { l.Log(LvlWarn, msg, adHocFields) }
// Error will create a new log with the Error level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Error(msg string, adHocFields ...LogFields) { l.Log(LvlError, msg, adHocFields) }
// Fatal will create a new log with the Fatal level, using the
// given adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Fatal(msg string, adHocFields ...LogFields) { l.Log(LvlFatal, msg, adHocFields) }
// ErrorFrom will create a new log with the Error level, using the
// given error and adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
//
// Note that this method will call the 'ErrorParser' configured
// function to extract the log message and custom fields from the
// given error. These custom fields will be overridden by the
// 'adHocFields' param
func (l *Logger) ErrorFrom(e error, adHocFields ...LogFields) {
msg, f := l.configuration.ErrorParser(e)
if f != nil {
l.Error(msg, append([]LogFields{f}, adHocFields...)...)
} else {
l.Error(msg, adHocFields...)
}
}
// FatalFrom will create a new log with the Fatal level, using the
// given error and adHocFields, if enabled.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
//
// Note that this method will call the 'ErrorParser' configured
// function to extract the log message and custom fields from the
// given error. These custom fields will be overridden by the
// 'adHocFields' param
func (l *Logger) FatalFrom(e error, adHocFields ...LogFields) {
msg, f := l.configuration.ErrorParser(e)
if f != nil {
l.Fatal(msg, append([]LogFields{f}, adHocFields...)...)
} else {
l.Fatal(msg, adHocFields...)
}
}
// Log is the base method that creates a new log, being used by all
// other log methods (Trace, Debug, Warn, ...). If there's the need
// to create custom log levels, use this method.
//
// Note that the 'adHocFields' param is variadic just to simulate
// optional params. Latter values will override former ones
func (l *Logger) Log(lvl uint64, msg string, adHocFields []LogFields) {
if notEnabled(l.configuration.LvlsEnabled, lvl) {
return
}
log := Log{
lvl,
msg,
l,
nil,
adHocFields,
nil,
}
if len(l.preHooks) > 0 {
log.preFields = LogFields{}
applyHooks(log, log.preFields, l.preHooks)
}
if l.configuration.AsyncScheduler != nil {
l.configuration.AsyncScheduler.NextChannel() <- log
} else {
handleLog(log)
}
}
const (
// LvlTrace is a flag that if used will enable
// Trace logs (via Logger Trace method)
LvlTrace uint64 = 1 << iota
// LvlDebug is a flag that if used will enable
// Debug logs (via Logger Debug method)
LvlDebug
// LvlInfo is a flag that if used will enable
// Info logs (via Logger Info method)
LvlInfo
// LvlWarn is a flag that if used will enable
// Warn logs (via Logger Warn method)
LvlWarn
// LvlError is a flag that if used will enable
// Error logs (via Logger Error method)
LvlError
// LvlFatal is a flag that if used will enable
// Fatal logs (via Logger Fatal method)
LvlFatal
)
const (
// LvlProduction will enable Info, Warn, Error and Fatal logs
LvlProduction = LvlInfo | LvlWarn | LvlError | LvlFatal
// LvlDefaults will enable Debug and all LvlProduction logs
LvlDefaults = LvlDebug | LvlProduction
// LvlAll will enable all logs
LvlAll = LvlTrace | LvlDefaults
)