-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
224 lines (191 loc) · 5.47 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
package slog
import (
"io"
"sort"
"time"
)
// Level specifies the log level.
type Level uint8
// All the log levels.
const (
DebugLevel Level = iota
InfoLevel
WarnLevel
ErrorLevel
CritLevel
disabledLevel Level = 255
)
var (
//cstLocation = time.FixedZone("CST", 60*60*8)
levelNames = []byte("DIWEC")
internalLogger *Logger
)
// A Record is an item which contains required context for the logger.
type Record struct {
level Level
time time.Time
message string
args []interface{}
}
// A Logger is a leveled logger with several handlers.
type Logger struct {
level Level // the lowest acceptable level
minLevel Level // the min level of its handlers
handlers []*Handler
internal bool
}
// NewLogger creates a new Logger of the given level.
// Messages with lower level than the logger will be ignored.
func NewLogger(lv Level) *Logger {
return &Logger{level: lv, minLevel: disabledLevel} // disable all levels for empty logger
}
// AddHandler adds a Handler to the Logger.
// A handler with lower level than the logger will be ignored.
func (l *Logger) AddHandler(h *Handler) {
if h.level < l.level {
return
}
h.internal = l.internal
l.handlers = append(l.handlers, h)
if len(l.handlers) > 1 {
if h.level < l.minLevel {
l.minLevel = h.level
}
sort.Slice(l.handlers, func(i, j int) bool {
return l.handlers[i].level < l.handlers[j].level
})
} else {
l.minLevel = h.level
}
}
// IsEnabledFor returns whether it's enabled for the level
func (l *Logger) IsEnabledFor(level Level) bool {
return l.minLevel <= level
}
// GetMinLevel returns its minLevel.
// Records lower than its minLevel will be ignored.
func (l *Logger) GetMinLevel() Level {
return l.minLevel
}
// Log logs a message with context.
// A logger should check the message level before call its Log().
// The line param should be uint32.
// It's not thread-safe, concurrent messages may be written in a random order
// through different handlers or writers.
// But two messages won't be mixed in a single line.
func (l *Logger) Log(lv Level, msg string, args ...interface{}) {
r := recordPool.Get().(*Record)
r.level = lv
//r.time = now().In(cstLocation)
r.time = now()
r.message = msg
r.args = args
for _, h := range l.handlers {
if !h.Handle(r) {
break
}
}
recordPool.Put(r)
}
// Close closes its handlers.
// It's safe to call this method more than once.
func (l *Logger) Close() {
for _, h := range l.handlers {
h.Close()
}
l.handlers = nil
}
// Debug logs a debug level message. It uses fmt.Fprint() to format args.
func (l *Logger) Debug(args ...interface{}) {
if l.IsEnabledFor(DebugLevel) {
l.Log(DebugLevel, "", args...)
}
}
// Debugf logs a debug level message. It uses fmt.Fprintf() to format msg and args.
func (l *Logger) Debugf(msg string, args ...interface{}) {
if l.IsEnabledFor(DebugLevel) {
l.Log(DebugLevel, msg, args...)
}
}
// Info logs a info level message. It uses fmt.Fprint() to format args.
func (l *Logger) Info(args ...interface{}) {
if l.IsEnabledFor(InfoLevel) {
l.Log(InfoLevel, "", args...)
}
}
// Infof logs a info level message. It uses fmt.Fprintf() to format msg and args.
func (l *Logger) Infof(msg string, args ...interface{}) {
if l.IsEnabledFor(InfoLevel) {
l.Log(InfoLevel, msg, args...)
}
}
// Warn logs a warning level message. It uses fmt.Fprint() to format args.
func (l *Logger) Warn(args ...interface{}) {
if l.IsEnabledFor(WarnLevel) {
l.Log(WarnLevel, "", args...)
}
}
// Warnf logs a warning level message. It uses fmt.Fprintf() to format msg and args.
func (l *Logger) Warnf(msg string, args ...interface{}) {
if l.IsEnabledFor(WarnLevel) {
l.Log(WarnLevel, msg, args...)
}
}
// Error logs an error level message. It uses fmt.Fprint() to format args.
func (l *Logger) Error(args ...interface{}) {
if l.IsEnabledFor(ErrorLevel) {
l.Log(ErrorLevel, "", args...)
}
}
// Errorf logs a error level message. It uses fmt.Fprintf() to format msg and args.
func (l *Logger) Errorf(msg string, args ...interface{}) {
if l.IsEnabledFor(ErrorLevel) {
l.Log(ErrorLevel, msg, args...)
}
}
// Crit logs a critical level message. It uses fmt.Fprint() to format args.
func (l *Logger) Crit(args ...interface{}) {
if l.IsEnabledFor(CritLevel) {
l.Log(CritLevel, "", args...)
}
}
// Critf logs a critical level message. It uses fmt.Fprintf() to format msg and args.
func (l *Logger) Critf(msg string, args ...interface{}) {
if l.IsEnabledFor(CritLevel) {
l.Log(CritLevel, msg, args...)
}
}
// NewLoggerWithWriter creates an info level logger with a writer.
func NewLoggerWithWriter(w io.WriteCloser) *Logger {
h := NewHandler(InfoLevel, DefaultFormatter)
h.AddWriter(w)
l := &Logger{level: InfoLevel}
l.AddHandler(h)
return l
}
// NewStdoutLogger creates a logger with a stdout writer.
func NewStdoutLogger() *Logger {
return NewLoggerWithWriter(NewStdoutWriter())
}
// NewStderrLogger creates a logger with a stderr writer.
func NewStderrLogger() *Logger {
return NewLoggerWithWriter(NewStderrWriter())
}
// SetInternalLogger sets a logger as the internalLogger which is used to log internal errors.
// The logger and its handlers will be marked as internal, so do not reuse them.
// The internalLogger may discard its own errors to prevent recursive log.
func SetInternalLogger(l *Logger) {
if internalLogger != nil {
internalLogger.internal = false
for _, h := range internalLogger.handlers {
h.internal = false
}
}
if l != nil {
l.internal = true
for _, h := range l.handlers {
h.internal = true
}
}
internalLogger = l
}