forked from go-ozzo/ozzo-log
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
300 lines (262 loc) · 8.58 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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package log implements logging with severity levels and message categories.
package log
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
// RFC5424 log message levels.
const (
LevelEmergency Level = iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInfo
LevelDebug
)
// Level describes the level of a log message.
type Level int
// LevelNames maps log levels to names
var LevelNames = map[Level]string{
LevelDebug: "Debug",
LevelInfo: "Info",
LevelNotice: "Notice",
LevelWarning: "Warning",
LevelError: "Error",
LevelCritical: "Critical",
LevelAlert: "Alert",
LevelEmergency: "Emergency",
}
// String returns the string representation of the log level
func (l Level) String() string {
if name, ok := LevelNames[l]; ok {
return name
}
return "Unknown"
}
// Entry represents a log entry.
type Entry struct {
Level Level
Category string
Message string
Time time.Time
CallStack string
FormattedMessage string
}
// String returns the string representation of the log entry
func (e *Entry) String() string {
return e.FormattedMessage
}
// Target represents a target where the logger can send log messages to for further processing.
type Target interface {
// Open prepares the target for processing log messages.
// Open will be invoked when Logger.Open() is called.
// If an error is returned, the target will be removed from the logger.
// errWriter should be used to write errors found while processing log messages.
Open(errWriter io.Writer) error
// Process processes an incoming log message.
Process(*Entry)
// Close closes a target.
// Close is called when Logger.Close() is called, which gives each target
// a chance to flush the logged messages to their destination storage.
Close()
}
// coreLogger maintains the log messages in a channel and sends them to various targets.
type coreLogger struct {
lock sync.RWMutex
open bool // whether the logger is open
entries chan *Entry // log entries
ErrorWriter io.Writer // the writer used to write errors caused by log targets
BufferSize int // the size of the channel storing log entries
CallStackDepth int // the number of call stack frames to be logged for each message. 0 means do not log any call stack frame.
CallStackFilter string // a substring that a call stack frame file path should contain in order for the frame to be counted
MaxLevel Level // the maximum level of messages to be logged
Targets []Target // targets for sending log messages to
}
// Formatter formats a log message into an appropriate string.
type Formatter func(*Logger, *Entry) string
// Logger records log messages and dispatches them to various targets for further processing.
type Logger struct {
*coreLogger
Category string // the category associated with this logger
Formatter Formatter // message formatter
}
// NewLogger creates a root logger.
// The new logger takes these default options:
// ErrorWriter: os.Stderr, BufferSize: 1024, MaxLevel: LevelDebug,
// Category: app, Formatter: DefaultFormatter
func NewLogger() *Logger {
logger := &coreLogger{
ErrorWriter: os.Stderr,
BufferSize: 1024,
MaxLevel: LevelDebug,
Targets: make([]Target, 0),
}
return &Logger{logger, "app", DefaultFormatter}
}
// GetLogger creates a logger with the specified category and log formatter.
// Messages logged through this logger will carry the same category name.
// The formatter, if not specified, will inherit from the calling logger.
// It will be used to format all messages logged through this logger.
func (l *Logger) GetLogger(category string, formatter ...Formatter) *Logger {
if len(formatter) > 0 {
return &Logger{l.coreLogger, category, formatter[0]}
}
return &Logger{l.coreLogger, category, l.Formatter}
}
// Emergency logs a message indicating the system is unusable.
// Please refer to Error() for how to use this method.
func (l *Logger) Emergency(format string, a ...interface{}) {
l.Log(LevelEmergency, format, a...)
}
// Alert logs a message indicating action must be taken immediately.
// Please refer to Error() for how to use this method.
func (l *Logger) Alert(format string, a ...interface{}) {
l.Log(LevelAlert, format, a...)
}
// Critical logs a message indicating critical conditions.
// Please refer to Error() for how to use this method.
func (l *Logger) Critical(format string, a ...interface{}) {
l.Log(LevelCritical, format, a...)
}
// Error logs a message indicating an error condition.
// This method takes one or multiple parameters. If a single parameter
// is provided, it will be treated as the log message. If multiple parameters
// are provided, they will be passed to fmt.Sprintf() to generate the log message.
func (l *Logger) Error(format string, a ...interface{}) {
l.Log(LevelError, format, a...)
}
// Warning logs a message indicating a warning condition.
// Please refer to Error() for how to use this method.
func (l *Logger) Warning(format string, a ...interface{}) {
l.Log(LevelWarning, format, a...)
}
// Notice logs a message meaning normal but significant condition.
// Please refer to Error() for how to use this method.
func (l *Logger) Notice(format string, a ...interface{}) {
l.Log(LevelNotice, format, a...)
}
// Info logs a message for informational purpose.
// Please refer to Error() for how to use this method.
func (l *Logger) Info(format string, a ...interface{}) {
l.Log(LevelInfo, format, a...)
}
// Debug logs a message for debugging purpose.
// Please refer to Error() for how to use this method.
func (l *Logger) Debug(format string, a ...interface{}) {
l.Log(LevelDebug, format, a...)
}
// Log logs a message of a specified severity level.
func (l *Logger) Log(level Level, format string, a ...interface{}) {
l.lock.RLock()
defer l.lock.RUnlock()
if level > l.MaxLevel || !l.open {
return
}
message := format
if len(a) > 0 {
message = fmt.Sprintf(format, a...)
}
entry := &Entry{
Category: l.Category,
Level: level,
Message: message,
Time: time.Now(),
}
if l.CallStackDepth > 0 {
entry.CallStack = GetCallStack(3, l.CallStackDepth, l.CallStackFilter)
}
entry.FormattedMessage = l.Formatter(l, entry)
l.entries <- entry
}
// Open prepares the logger and the targets for logging purpose.
// Open must be called before any message can be logged.
func (l *coreLogger) Open() error {
l.lock.Lock()
defer l.lock.Unlock()
if l.open {
return nil
}
if l.ErrorWriter == nil {
return errors.New("Logger.ErrorWriter must be set.")
}
if l.BufferSize < 0 {
return errors.New("Logger.BufferSize must be no less than 0.")
}
if l.CallStackDepth < 0 {
return errors.New("Logger.CallStackDepth must be no less than 0.")
}
l.entries = make(chan *Entry, l.BufferSize)
var targets []Target
for _, target := range l.Targets {
if err := target.Open(l.ErrorWriter); err != nil {
fmt.Fprintf(l.ErrorWriter, "Failed to open target: %v", err)
} else {
targets = append(targets, target)
}
}
l.Targets = targets
go l.process()
l.open = true
return nil
}
// process sends the messages to targets for processing.
func (l *coreLogger) process() {
for {
entry := <-l.entries
for _, target := range l.Targets {
target.Process(entry)
}
if entry == nil {
break
}
}
}
// Close closes the logger and the targets.
// Existing messages will be processed before the targets are closed.
// New incoming messages will be discarded after calling this method.
func (l *coreLogger) Close() {
l.lock.Lock()
defer l.lock.Unlock()
if !l.open {
return
}
l.open = false
// use a nil entry to signal the close of logger
l.entries <- nil
for _, target := range l.Targets {
target.Close()
}
}
// DefaultFormatter is the default formatter used to format every log message.
func DefaultFormatter(l *Logger, e *Entry) string {
return fmt.Sprintf("%v [%v][%v] %v%v", e.Time.Format(time.RFC3339), e.Level, e.Category, e.Message, e.CallStack)
}
// GetCallStack returns the current call stack information as a string.
// The skip parameter specifies how many top frames should be skipped, while
// the frames parameter specifies at most how many frames should be returned.
func GetCallStack(skip int, frames int, filter string) string {
buf := new(bytes.Buffer)
for i, count := skip, 0; count < frames; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if filter == "" || strings.Contains(file, filter) {
fmt.Fprintf(buf, "\n%s:%d", file, line)
count++
}
}
return buf.String()
}