-
Notifications
You must be signed in to change notification settings - Fork 2
/
default.go
326 lines (274 loc) · 7.12 KB
/
default.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
package gol
import (
"bytes"
"fmt"
"io"
"sync"
"time"
)
const packageSeparator = '/'
// Level represents logging level.
type Level int
// Log levels
const (
Uninitialized Level = iota
All
Trace
Debug
Info
Warn
Error
Off
)
const (
// RootLoggerName is the name of the root logger.
RootLoggerName = "root"
)
var levelStrings = map[Level]string{
All: "ALL",
Trace: "TRACE",
Debug: "DEBUG",
Info: "INFO",
Warn: "WARN",
Error: "ERROR",
Off: "OFF",
}
// LevelString returns the text for the level.
func LevelString(level Level) string {
return levelStrings[level]
}
// LoggingEvent is the representation of logging events.
type LoggingEvent struct {
// Name is the of the logger.
Name string
// Level is the current logger level.
Level Level
// Time is when the logging happens.
Time time.Time
Message bytes.Buffer
}
var eventPool = sync.Pool{}
func newLoggingEvent() *LoggingEvent {
e := eventPool.Get()
if e != nil {
return e.(*LoggingEvent)
}
return &LoggingEvent{}
}
func releaseLoggingEvent(e *LoggingEvent) {
e.Message.Reset()
eventPool.Put(e)
}
// Appender appends contents to a Writer.
type Appender interface {
Append(*LoggingEvent)
}
// DefaultAppender implements Appender interface.
type DefaultAppender struct {
timeLayout string
target io.Writer
}
// NewAppender allocates and returns a new DefaultAppender.
func NewAppender(target io.Writer) *DefaultAppender {
return &DefaultAppender{
timeLayout: "2006-01-02T15:04:05.000Z07:00", // ISO8601 with milliseconds.
target: target,
}
}
// Append uses its encoder to send the event to the target.
func (appender *DefaultAppender) Append(event *LoggingEvent) {
if appender.target == nil {
return
}
// Use buffer from a logging event
e := newLoggingEvent()
defer releaseLoggingEvent(e)
buf := &e.Message
// Level (minimum 5 characters)
level := LevelString(event.Level)
n, _ := buf.WriteString(level)
for n = 5 - n; n > 0; n-- {
buf.WriteByte(' ')
}
// Time
buf.WriteByte(' ')
buf.WriteByte('[')
var timeBuf [64]byte
buf.Write(event.Time.AppendFormat(timeBuf[:0], appender.timeLayout))
buf.WriteByte(']')
// Logger name
buf.WriteByte(' ')
buf.WriteString(event.Name)
buf.WriteByte(':')
// Logging message in the end
buf.WriteByte(' ')
buf.Write(event.Message.Bytes())
buf.WriteByte('\n')
_, err := buf.WriteTo(appender.target)
if err != nil {
Print(err)
}
}
// DefaultLogger implements Logger interface.
type DefaultLogger struct {
name string
level Level
appender Appender
parent *DefaultLogger
}
// New allocates and returns a new DefaultLogger.
// This method should not be called directly in application, use
// LoggerFactory.GetLogger() instead as a DefaultLogger requires
// Appender from itself or its parent.
func New(name string, parent *DefaultLogger) *DefaultLogger {
return &DefaultLogger{
name: name,
level: Uninitialized,
parent: parent,
}
}
// Tracef logs message at Trace level.
func (logger *DefaultLogger) Tracef(format string, args ...interface{}) {
logger.Printf(Trace, format, args)
}
// TraceEnabled checks if Trace level is enabled.
func (logger *DefaultLogger) TraceEnabled() bool {
return logger.loggable(Trace)
}
// Debugf logs message at Debug level.
func (logger *DefaultLogger) Debugf(format string, args ...interface{}) {
logger.Printf(Debug, format, args)
}
// DebugEnabled checks if Debug level is enabled.
func (logger *DefaultLogger) DebugEnabled() bool {
return logger.loggable(Debug)
}
// Infof logs message at Info level.
func (logger *DefaultLogger) Infof(format string, args ...interface{}) {
logger.Printf(Info, format, args)
}
// InfoEnabled checks if Info level is enabled.
func (logger *DefaultLogger) InfoEnabled() bool {
return logger.loggable(Info)
}
// Warnf logs message at Warning level.
func (logger *DefaultLogger) Warnf(format string, args ...interface{}) {
logger.Printf(Warn, format, args)
}
// WarnEnabled checks if Warning level is enabled.
func (logger *DefaultLogger) WarnEnabled() bool {
return logger.loggable(Warn)
}
// Errorf logs message at Error level.
func (logger *DefaultLogger) Errorf(format string, args ...interface{}) {
logger.Printf(Error, format, args)
}
// ErrorEnabled checks if Error level is enabled.
func (logger *DefaultLogger) ErrorEnabled() bool {
return logger.loggable(Error)
}
// Level returns level of this logger or parent if not set.
func (logger *DefaultLogger) Level() Level {
for logger != nil {
if logger.level != Uninitialized {
return logger.level
}
logger = logger.parent
}
return Off
}
// SetLevel changes logging level of this logger.
func (logger *DefaultLogger) SetLevel(level Level) {
logger.level = level
}
// Appender returns appender of this logger or parent if not set.
func (logger *DefaultLogger) Appender() Appender {
for logger != nil {
if logger.appender != nil {
return logger.appender
}
logger = logger.parent
}
return nil
}
// SetAppender changes appender of this logger.
func (logger *DefaultLogger) SetAppender(appender Appender) {
logger.appender = appender
}
// loggable checks if the given logging level is enabled within this logger.
func (logger *DefaultLogger) loggable(level Level) bool {
return level >= logger.Level()
}
// log performs logging with given parameters.
func (logger *DefaultLogger) Printf(level Level, format string, args []interface{}) {
if !logger.loggable(level) {
return
}
appender := logger.Appender()
if appender == nil {
return
}
event := newLoggingEvent()
defer releaseLoggingEvent(event)
event.Time = time.Now()
event.Name = logger.name
event.Level = level
fmt.Fprintf(&event.Message, format, args...)
appender.Append(event)
}
// DefaultFactory implements Factory interface.
type DefaultFactory struct {
root *DefaultLogger
mu sync.RWMutex
loggers map[string]*DefaultLogger
}
// NewFactory allocates and returns new DefaultFactory.
func NewFactory(writer io.Writer) *DefaultFactory {
rootLogger := New(RootLoggerName, nil)
rootLogger.SetLevel(Info)
rootLogger.SetAppender(NewAppender(writer))
return &DefaultFactory{
root: rootLogger,
loggers: map[string]*DefaultLogger{
RootLoggerName: rootLogger,
},
}
}
// GetLogger returns a new Logger or an existing one if the same name is found.
func (factory *DefaultFactory) GetLogger(name string) Logger {
if name == "" {
return factory.root
}
factory.mu.RLock()
logger, ok := factory.loggers[name]
factory.mu.RUnlock()
if !ok {
factory.mu.Lock()
logger = factory.createLogger(name, factory.getParent(name))
factory.mu.Unlock()
}
return logger
}
// getParent returns parent logger for given logger.
func (factory *DefaultFactory) getParent(name string) *DefaultLogger {
parent := factory.root
for i, c := range name {
// Search for package separator character
if c == packageSeparator {
parentName := name[0:i]
if parentName != "" {
parent = factory.createLogger(parentName, parent)
}
}
}
return parent
}
// createLogger creates a new logger if not exist.
func (factory *DefaultFactory) createLogger(name string, parent *DefaultLogger) *DefaultLogger {
logger, ok := factory.loggers[name]
if !ok {
logger = New(name, parent)
factory.loggers[name] = logger
}
return logger
}