-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler.go
159 lines (139 loc) · 4.02 KB
/
handler.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
package logging
import (
"sync"
)
// An interface for dispatching logging events to specific destinations.
// Handler can optionally use formatter instances to format records as
// desired.
type Handler interface {
// Return the name of Handler.
GetName() string
// Set the name of Handler.
SetName(name string)
// Return the log level of Handler.
GetLevel() LogLevelType
// Set the log level of Handler.
SetLevel(level LogLevelType) error
// For Formatter.
// Format the specified record.
Formatter
// Set the formatter for this Handler.
SetFormatter(formatter Formatter)
// For Filter managing.
Filterer
// Do whatever it takes to actually log the specified logging record.
Emit(record *LogRecord) error
// Conditionally emit the specified logging record.
Handle(record *LogRecord) int
// Handle errors which occur during an Emit() call.
HandleError(record *LogRecord, err error)
// Ensure all logging output has been flushed.
Flush() error
// Tidy up any resources used by the handler.
Close()
}
// The base handler class. Acts as a base parent of any concrete handler class.
// By default, no formatter is specified, in this case, the "raw" message as
// determined by record.Message is logged.
type BaseHandler struct {
*StandardFilterer
name string
nameLock sync.RWMutex
level LogLevelType
levelLock sync.RWMutex
formatter Formatter
formatterLock sync.RWMutex
lock sync.Mutex
}
// Initialize the instance - basically setting the formatter to nil and the
// filterer without filter.
func NewBaseHandler(name string, level LogLevelType) *BaseHandler {
return &BaseHandler{
StandardFilterer: NewStandardFilterer(),
name: name,
level: level,
formatter: nil,
}
}
func (self *BaseHandler) GetName() string {
self.nameLock.RLock()
defer self.nameLock.RUnlock()
return self.name
}
func (self *BaseHandler) SetName(name string) {
self.nameLock.Lock()
defer self.nameLock.Unlock()
self.name = name
}
func (self *BaseHandler) GetLevel() LogLevelType {
self.levelLock.RLock()
defer self.levelLock.RUnlock()
return self.level
}
func (self *BaseHandler) SetLevel(level LogLevelType) error {
self.levelLock.Lock()
defer self.levelLock.Unlock()
_, ok := getLevelName(level)
if !ok {
return ErrorNoSuchLevel
}
self.level = level
return nil
}
func (self *BaseHandler) SetFormatter(formatter Formatter) {
self.formatterLock.Lock()
defer self.formatterLock.Unlock()
self.formatter = formatter
}
// Acquire a lock for serializing access to the underlying I/O.
func (self *BaseHandler) Lock() {
self.lock.Lock()
}
// Release the I/O lock.
func (self *BaseHandler) Unlock() {
self.lock.Unlock()
}
// Format the specified record.
// If a formatter is set, use it. Otherwise, use the default formatter
// for the module.
func (self *BaseHandler) Format(record *LogRecord) string {
self.formatterLock.RLock()
defer self.formatterLock.RUnlock()
var formatter Formatter
if self.formatter != nil {
formatter = self.formatter
} else {
formatter = defaultFormatter
}
return formatter.Format(record)
}
// A helper function for any subclass to define its Handle() method.
// Logging event emission depends on filters which may have heen added to
// the handler. Wrap the actual emission of the record and error handling
// with Lock()/Unlock() of the I/O lock. Returns non-zero if the filter passed
// the record for emission, else zero.
func (self *BaseHandler) Handle2(handler Handler, record *LogRecord) int {
rv := handler.Filter(record)
if rv > 0 {
self.Lock()
defer self.Unlock()
err := handler.Emit(record)
if err != nil {
handler.HandleError(record, err)
}
}
return rv
}
// A doing-nothing implementation as a stub for any subclass.
func (self *BaseHandler) HandleError(_ *LogRecord, _ error) {
// Empty body
}
// A doing-nothing implementation as a stub for any subclass.
func (self *BaseHandler) Flush() error {
// Empty body
return nil
}
// A doing-nothing implementation as a stub for any subclass.
func (self *BaseHandler) Close() {
// Empty body
}