-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.go
413 lines (357 loc) · 11.3 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
// Copyright (c) 2022 xybor-x
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package xylog
import (
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/xybor-x/xycond"
"github.com/xybor-x/xylock"
)
// Logger represents a single logging channel. A "logging channel" indicates an
// area of an application. Exactly how an "area" is defined is up to the
// application developer. Since an application can have any number of areas,
// logging channels are identified by a unique string. Application areas can be
// nested (e.g. an area of "input processing" might include sub-areas "read CSV
// files", "read XLS files" and "read Gnumeric files"). To cater for this
// natural nesting, channel names are organized into a namespace hierarchy where
// levels are separated by periods. So in the instance given above, channel
// names might be "input" for the upper level, and "input.csv", "input.xls" and
// "input.gnu" for the sub-levels. There is no arbitrary limit to the depth of
// nesting.
type Logger struct {
f *filterer
name string
children map[string]*Logger
parent *Logger
level int
handlers []*Handler
lock *xylock.RWLock
cache map[int]bool
fields []field
}
// GetLogger gets a logger with the specified name, creating it if it doesn't
// yet exist. This name is a dot-separated hierarchical name, such as "a",
// "a.b", "a.b.c", or similar.
//
// Leave name as empty string to get the root logger.
func GetLogger(name string) *Logger {
if name == "" {
return rootLogger
}
globalLock.Lock()
defer globalLock.Unlock()
var lg = rootLogger
for _, part := range strings.Split(name, ".") {
if _, ok := lg.children[part]; !ok {
lg.children[part] = newLogger(part, lg)
}
lg = lg.children[part]
}
return lg
}
// Name returns the full name.
func (lg *Logger) Name() string {
return lg.lock.RLockFunc(func() any { return lg.name }).(string)
}
// Parent returns the parent logger. If there is no parent, return nil instead.
func (lg *Logger) Parent() *Logger {
lg.lock.RLock()
defer lg.lock.RUnlock()
return lg.parent
}
// Children returns direct children logger.
func (lg *Logger) Children() []*Logger {
var children []*Logger
lg.lock.RLock()
defer lg.lock.RUnlock()
for _, child := range lg.children {
children = append(children, child)
}
return children
}
// Level returns the current logging level.
func (lg *Logger) Level() int {
lg.lock.RLock()
defer lg.lock.RUnlock()
return lg.level
}
// SetLevel sets the new logging level.
func (lg *Logger) SetLevel(level int) {
lg.lock.WLockFunc(func() { lg.level = level })
rootLogger.clearCache()
}
// Handlers returns all current Handlers.
func (lg *Logger) Handlers() []*Handler {
lg.lock.RLock()
defer lg.lock.RUnlock()
return lg.handlers
}
// AddHandler adds a new handler.
func (lg *Logger) AddHandler(h *Handler) {
xycond.AssertNotNil(h)
lg.lock.WLockFunc(func() { lg.handlers = append(lg.handlers, h) })
}
// RemoveHandler removes an existed Handler.
func (lg *Logger) RemoveHandler(h *Handler) {
lg.lock.Lock()
defer lg.lock.Unlock()
for i := range lg.handlers {
if lg.handlers[i] == h {
lg.handlers = append(lg.handlers[:i], lg.handlers[i+1:]...)
}
}
}
// RemoveAllHandlers removes all existed Handlers.
func (lg *Logger) RemoveAllHandlers() {
lg.lock.Lock()
defer lg.lock.Unlock()
lg.handlers = lg.handlers[:0]
}
// Filters returns all current Filters.
func (lg *Logger) Filters() []Filter {
lg.lock.RLock()
defer lg.lock.RUnlock()
return lg.f.Filters()
}
// AddFilter adds a specified Filter.
func (lg *Logger) AddFilter(f Filter) {
lg.lock.WLockFunc(func() { lg.f.AddFilter(f) })
}
// RemoveFilter removes an existed Filter.
func (lg *Logger) RemoveFilter(f Filter) {
lg.lock.Lock()
defer lg.lock.Unlock()
lg.f.RemoveFilter(f)
}
// AddField adds a fixed field to all logging message of this logger.
func (lg *Logger) AddField(key string, value any) {
lg.lock.WLockFunc(func() {
lg.fields = append(lg.fields, makeField(key, value))
})
}
// Debug logs default formatting objects with DEBUG level.
func (lg *Logger) Debug(s string) {
if lg.isEnabledFor(DEBUG) {
lg.log(DEBUG, makeField("messsage", s))
}
}
// Debugf logs a formatting message with DEBUG level.
func (lg *Logger) Debugf(s string, a ...any) {
if lg.isEnabledFor(DEBUG) {
lg.log(DEBUG, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Info logs default formatting objects with INFO level.
func (lg *Logger) Info(s string) {
if lg.isEnabledFor(INFO) {
lg.log(INFO, makeField("messsage", s))
}
}
// Infof logs a formatting message with INFO level.
func (lg *Logger) Infof(s string, a ...any) {
if lg.isEnabledFor(INFO) {
lg.log(INFO, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Warn logs default formatting objects with WARN level.
func (lg *Logger) Warn(s string) {
if lg.isEnabledFor(WARN) {
lg.log(WARN, makeField("messsage", s))
}
}
// Warnf logs a formatting message with WARN level.
func (lg *Logger) Warnf(s string, a ...any) {
if lg.isEnabledFor(WARN) {
lg.log(WARN, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Warning logs default formatting objects with WARNING level.
func (lg *Logger) Warning(s string) {
if lg.isEnabledFor(WARNING) {
lg.log(WARNING, makeField("messsage", s))
}
}
// Warningf logs a formatting message with WARNING level.
func (lg *Logger) Warningf(s string, a ...any) {
if lg.isEnabledFor(WARNING) {
lg.log(WARNING, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Error logs default formatting objects with ERROR level.
func (lg *Logger) Error(s string) {
if lg.isEnabledFor(ERROR) {
lg.log(ERROR, makeField("messsage", s))
}
}
// Errorf logs a formatting message with ERROR level.
func (lg *Logger) Errorf(s string, a ...any) {
if lg.isEnabledFor(ERROR) {
lg.log(ERROR, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Critical logs default formatting objects with CRITICAL level.
func (lg *Logger) Critical(s string) {
if lg.isEnabledFor(CRITICAL) {
lg.log(CRITICAL, makeField("messsage", s))
}
}
// Criticalf logs a formatting message with CRITICAL level.
func (lg *Logger) Criticalf(s string, a ...any) {
if lg.isEnabledFor(CRITICAL) {
lg.log(CRITICAL, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Fatal logs default formatting objects with CRITICAL level, then followed by a
// call to os.Exit(1).
func (lg *Logger) Fatal(s string) {
lg.Critical(s)
os.Exit(1)
}
// Fatalf logs a formatting message with CRITICAL level, then followed by a call
// to os.Exit(1).
func (lg *Logger) Fatalf(s string, a ...any) {
lg.Criticalf(s, a...)
os.Exit(1)
}
// Panic logs default formatting objects with CRITICAL level, then followed by a
// call to panic().
func (lg *Logger) Panic(s string) {
lg.Critical(s)
panic(s)
}
// Panicf logs a formatting message with CRITICAL level, then followed by a call
// to panic().
func (lg *Logger) Panicf(s string, a ...any) {
lg.Criticalf(s, a...)
panic(fmt.Sprintf(s, a...))
}
// Log logs default formatting objects with a custom level.
func (lg *Logger) Log(level int, s string) {
level = CheckLevel(level)
if lg.isEnabledFor(level) {
lg.log(level, makeField("messsage", s))
}
}
// Logf logs a formatting message with a custom level.
func (lg *Logger) Logf(level int, s string, a ...any) {
level = CheckLevel(level)
if lg.isEnabledFor(level) {
lg.log(level, makeField("messsage", fmt.Sprintf(s, a...)))
}
}
// Stack logs the stack trace.
func (lg *Logger) Stack(level int) {
var s = string(debug.Stack())
var lines = strings.Split(s, "\n")
for i := range lines {
lg.log(level, makeField("stack", strings.TrimSpace(lines[i])))
}
}
// Event creates an EventLogger which logs key-value pairs.
func (lg *Logger) Event(e string) *EventLogger {
var elogger = eventLoggerPool.Get().(*EventLogger)
elogger.lg = lg
elogger.Field("event", e)
return elogger
}
// log is a low-level logging method which creates a LogRecord and then calls
// all the handlers of this logger to handle the record.
func (lg *Logger) log(level int, fields ...field) {
fields = append(fields, lg.fields...)
var record = makeRecord(lg.name, level, fields...)
if lg.filter(record) {
lg.callHandlers(record)
}
}
// filter checks all filters in filterer, if there is any failed filter, it will
// returns false.
func (lg *Logger) filter(r LogRecord) bool {
return lg.lock.RLockFunc(func() any { return lg.f.filter(r) }).(bool)
}
// callHandlers passes a record to all relevant handlers.
//
// Loop through all handlers for this logger and its parents in the logger
// hierarchy. If no handler was found, output a one-off error message to
// os.Stderr.
func (lg *Logger) callHandlers(record LogRecord) {
var current = lg
for current != nil {
var handlers = current.Handlers()
for i := range handlers {
handlers[i].Handle(record)
}
current = current.Parent()
}
}
// isEnabledFor checks if a logging level should be logged in this logger.
func (lg *Logger) isEnabledFor(level int) bool {
lg.lock.RLock()
var isEnabled, isCached = lg.cache[level]
lg.lock.RUnlock()
if !isCached {
isEnabled = level >= lg.getEffectiveLevel()
lg.lock.WLockFunc(func() { lg.cache[level] = isEnabled })
}
return isEnabled
}
// getEffectiveLevel gets the effective level for this logger.
//
// Loop through this logger and its parents in the logger hierarchy, looking for
// a non-zero logging level. Return the first one found.
func (lg *Logger) getEffectiveLevel() int {
var level, parent = lg.Level(), lg.Parent()
if level != NOTSET || parent == nil {
return level
}
return parent.getEffectiveLevel()
}
// clearCache clears logging level cache of this logger and all its children.
func (lg *Logger) clearCache() {
lg.lock.WLockFunc(func() {
for k := range lg.cache {
delete(lg.cache, k)
}
})
for _, child := range lg.Children() {
child.clearCache()
}
}
// newLogger creates a Logger with a name and parent. The fullname of logger
// will be concatenated by the parent's fullname. This logger will not be
// automatically added to logger hierarchy. The returned logger has no child,
// no handler, and NOTSET level.
func newLogger(name string, parent *Logger) *Logger {
var current = parent
if current != nil && current != rootLogger {
name = current.Name() + "." + name
}
return &Logger{
f: &filterer{},
name: name,
children: make(map[string]*Logger),
parent: parent,
level: NOTSET,
handlers: nil,
lock: &xylock.RWLock{},
cache: make(map[int]bool),
}
}