-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
299 lines (244 loc) · 5.05 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
package easylog
type Level int8
const (
DEBUG Level = iota - 1
INFO
WARN
ERROR
PANIC
FATAL
_MIN = DEBUG
_MAX = FATAL
)
func (l Level) String() string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARN:
return "WARN"
case ERROR:
return "ERROR"
case PANIC:
return "PANIC"
case FATAL:
return "FATAL"
default:
return "UNKNOWN"
}
}
// Logger is not thread safe
// Make sure to configure the Logger before emitting logs,
// And do not reconfigure the Logger during runtime.
type Logger struct {
manager *manager
parent *Logger
placeholder bool
children map[*Logger]struct{}
name string
propagate bool
level Level
handlers []Handler
errorHandler ErrorHandler
caller map[Level]bool
stack map[Level]bool
tags map[interface{}]interface{}
kvs map[interface{}]interface{}
}
func newLogger() *Logger {
return &Logger{
children: make(map[*Logger]struct{}),
handlers: make([]Handler, 0),
errorHandler: NewNopErrorHandler(),
caller: make(map[Level]bool),
stack: make(map[Level]bool),
tags: make(map[interface{}]interface{}),
kvs: make(map[interface{}]interface{}),
}
}
func (l *Logger) Name() string {
return l.name
}
func (l *Logger) SetPropagate(propagate bool) {
l.propagate = propagate
}
func (l *Logger) GetPropagate() bool {
return l.propagate
}
func (l *Logger) SetLevel(level Level) {
l.level = level
}
func (l *Logger) GetLevel() Level {
return l.level
}
func (l *Logger) AddHandler(h Handler) {
if h == nil {
return
}
for _, handler := range l.handlers {
if handler == h {
return
}
}
l.handlers = append(l.handlers, h)
}
func (l *Logger) RemoveHandler(h Handler) {
if h == nil {
return
}
for i, handler := range l.handlers {
if handler == h {
l.handlers = append(l.handlers[:i], l.handlers[i+1:]...)
return
}
}
}
func (l *Logger) ResetHandler() {
l.handlers = make([]Handler, 0)
}
func (l *Logger) SetErrorHandler(w ErrorHandler) {
l.errorHandler = w
}
func (l *Logger) EnableCaller(level Level) {
if level >= _MIN && level <= _MAX {
l.caller[level] = true
}
}
func (l *Logger) DisableCaller(level Level) {
if level >= _MIN && level <= _MAX {
l.caller[level] = false
}
}
func (l *Logger) EnableStack(level Level) {
if level >= _MIN && level <= _MAX {
l.stack[level] = true
}
}
func (l *Logger) DisableStack(level Level) {
if level >= _MIN && level <= _MAX {
l.stack[level] = false
}
}
func (l *Logger) SetTag(k interface{}, v interface{}) {
l.tags[k] = v
}
func (l *Logger) DelTag(k interface{}) {
delete(l.tags, k)
}
func (l *Logger) ResetTag() {
l.tags = make(map[interface{}]interface{})
}
func (l *Logger) Tags() map[interface{}]interface{} {
return l.tags
}
func (l *Logger) SetKv(k interface{}, v interface{}) {
l.kvs[k] = v
}
func (l *Logger) DelKv(k interface{}) {
delete(l.kvs, k)
}
func (l *Logger) ResetKv() {
l.kvs = make(map[interface{}]interface{})
}
func (l *Logger) Kvs() map[interface{}]interface{} {
return l.kvs
}
func (l *Logger) Debug() *Event {
return l.log(DEBUG, nil)
}
func (l *Logger) Info() *Event {
return l.log(INFO, nil)
}
func (l *Logger) Warn() *Event {
return l.log(WARN, nil)
}
func (l *Logger) Error() *Event {
return l.log(ERROR, nil)
}
func (l *Logger) Panic() *Event {
return l.log(PANIC, func(v interface{}) {
l.Flush()
panic(v)
})
}
func (l *Logger) Fatal() *Event {
return l.log(FATAL, func(v interface{}) {
l.Flush()
l.Close()
exit(1)
})
}
func (l *Logger) Flush() {
for _, handler := range l.handlers {
if err := handler.Flush(); err != nil {
// ignore error produced by errorHandler
_ = l.errorHandler.Handle(err)
}
}
// ignore error produced by errorHandler
_ = l.errorHandler.Flush()
}
func (l *Logger) Close() {
for _, handler := range l.handlers {
if err := handler.Close(); err != nil {
// ignore error produced by errorHandler
_ = l.errorHandler.Handle(err)
}
}
// ignore error produced by errorHandler
_ = l.errorHandler.Close()
}
func (l *Logger) logCaller(level Level) bool {
if need, ok := l.caller[level]; ok {
return need
}
return false
}
func (l *Logger) logStack(level Level) bool {
if need, ok := l.stack[level]; ok {
return need
}
return false
}
// couldEnd could end the Logger with panic or os.exit().
func (l *Logger) couldEnd(level Level, v interface{}) {
// Note: If there is any level bigger than PANIC added, the logic here should be updated.
switch level {
case PANIC:
l.Flush()
panic(v)
case FATAL:
l.Flush()
l.Close()
exit(1)
}
}
func (l *Logger) log(level Level, done func(interface{})) *Event {
if level < l.level {
if done != nil {
done("")
}
return nil
}
return newEvent(l, level)
}
func (l *Logger) handle(event *Event) {
defer event.Put()
if event.level < l.level {
return
}
for _, handler := range l.handlers {
next, err := handler.Handle(event)
if err != nil {
// ignore error produced by errorHandler
_ = l.errorHandler.Handle(err)
}
if !next {
return
}
}
if l.propagate && l.parent != nil {
l.parent.handle(event)
}
}