-
Notifications
You must be signed in to change notification settings - Fork 23
/
init.go
113 lines (94 loc) · 2.44 KB
/
init.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
package logging
import (
"sync"
)
type HandlerCloser struct {
handlers *ListSet
lock sync.Mutex
}
func NewHandlerCloser() *HandlerCloser {
return &HandlerCloser{
handlers: NewListSet(),
}
}
func (self *HandlerCloser) AddHandler(handler Handler) {
self.lock.Lock()
defer self.lock.Unlock()
if !self.handlers.SetContains(handler) {
self.handlers.SetAdd(handler)
}
}
func (self *HandlerCloser) RemoveHandler(handler Handler) {
self.lock.Lock()
defer self.lock.Unlock()
if self.handlers.SetContains(handler) {
self.handlers.SetRemove(handler)
}
}
func (self *HandlerCloser) Close() {
self.lock.Lock()
defer self.lock.Unlock()
for e := self.handlers.Front(); e != nil; e = e.Next() {
handler, _ := e.Value.(Handler)
handler.Close()
}
}
var (
root Logger
manager *Manager
Closer *HandlerCloser
)
func init() {
initialize()
}
func initialize() {
root = NewRootLogger(LevelWarn)
manager = NewManager(root)
Closer = NewHandlerCloser()
}
// Ensure all log messages are flushed before program exits.
func Shutdown() {
Closer.Close()
initialize()
}
// Set logger maker for default manager.
func SetLoggerMaker(maker LoggerMaker) {
manager.SetLoggerMaker(maker)
}
// Return a logger with the specified name, creating it if necessary.
// If empty name is specified, return the root logger.
func GetLogger(name string) Logger {
if len(name) > 0 {
return manager.GetLogger(name)
} else {
return root
}
}
// Log a message with severity "LevelFatal" on the root logger.
func Fatalf(format string, args ...interface{}) {
root.Fatalf(format, args...)
}
// Log a message with severity "LevelError" on the root logger.
func Errorf(format string, args ...interface{}) {
root.Errorf(format, args...)
}
// Log a message with severity "LevelWarn" on the root logger.
func Warnf(format string, args ...interface{}) {
root.Warnf(format, args...)
}
// Log a message with severity "LevelInfo" on the root logger.
func Infof(format string, args ...interface{}) {
root.Infof(format, args...)
}
// Log a message with severity "LevelDebug" on the root logger.
func Debugf(format string, args ...interface{}) {
root.Debugf(format, args...)
}
// Log a message with severity "LevelTrace" on the root logger.
func Tracef(format string, args ...interface{}) {
root.Tracef(format, args...)
}
// Log a message with specified severity level on the root logger.
func Logf(level LogLevelType, format string, args ...interface{}) {
root.Logf(level, format, args...)
}