-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.go
355 lines (299 loc) · 11.5 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2020 The ZKits Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logger
import (
"io"
stdlog "log"
"os"
"sync/atomic"
"time"
"github.com/edoger/zkits-logger/internal"
)
// Logger interface defines a standard logger.
type Logger interface {
Log
// GetLevel returns the current logger level.
GetLevel() Level
// SetLevel sets the current logger level.
// When the given log level is invalid, this method does nothing.
SetLevel(Level) Logger
// SetLevelString sets the current logger level by string.
SetLevelString(s string) error
// ForceSetLevelString sets the current logger level by string.
// When the given log level string is invalid, this method does nothing.
ForceSetLevelString(s string) Logger
// SetOutput sets the current logger output writer.
// If the given writer is nil, os.Stdout is used.
SetOutput(io.Writer) Logger
// SetLevelOutput sets the current logger level output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the level writer will be disabled.
SetLevelOutput(Level, io.Writer) Logger
// SetLevelsOutput sets the current logger levels output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the levels writer will be disabled.
SetLevelsOutput([]Level, io.Writer) Logger
// SetOutputInterceptor sets the output interceptor for the current logger.
// If the given interceptor is nil, the log data is written to the output writer.
SetOutputInterceptor(func(Summary, io.Writer) (int, error)) Logger
// SetNowFunc sets the function that gets the current time.
// If the given function is nil, time.Now is used.
SetNowFunc(func() time.Time) Logger
// SetExitFunc sets the exit function of the current logger.
// If the given function is nil, the exit function is disabled.
// The exit function is called automatically after the FatalLevel level log is recorded.
// By default, the exit function we use is os.Exit.
SetExitFunc(func(int)) Logger
// SetPanicFunc sets the panic function of the current logger.
// If the given function is nil, the panic function is disabled.
// The panic function is called automatically after the PanicLevel level log is recorded.
// By default, the panic function we use is func(s string) { panic(s) }.
SetPanicFunc(func(string)) Logger
// SetFormatter sets the log formatter for the current logger.
// If the given log formatter is nil, we will record the log in JSON format.
SetFormatter(Formatter) Logger
// SetFormatOutput sets the log format output.
// After setting the format output, the format and output of the logger will be controlled by this structure,
// and the bound log output and log level output will no longer be used.
// If format output needs to be disabled, set to nil and the logger will back to the original behavior.
SetFormatOutput(FormatOutput) Logger
// SetDefaultTimeFormat sets the default log time format for the current logger.
// If the given time format is empty string, internal.DefaultTimeFormat is used.
SetDefaultTimeFormat(string) Logger
// EnableCaller enables caller reporting on all levels of logs.
EnableCaller(...int) Logger
// EnableLevelCaller enables caller reporting on logs of a given level.
EnableLevelCaller(Level, ...int) Logger
// EnableLevelsCaller enables caller reporting on logs of the given levels.
EnableLevelsCaller([]Level, ...int) Logger
// SetCallerSkip sets a fixed number of skipped callers.
// This method only takes effect for the log with caller enabled.
SetCallerSkip(int) Logger
// SetLongCaller sets whether to enable or disable long caller name (with parent directory name).
SetLongCaller(long bool) Logger
// AddHook adds the given log hook to the current logger.
AddHook(Hook) Logger
// AddHookFunc adds the given log hook function to the current logger.
AddHookFunc([]Level, func(Summary) error) Logger
// EnableHook enables or disables the log hook.
EnableHook(bool) Logger
// AsLog converts current Logger to Log instances, which is unidirectional.
AsLog() Log
// AsStandardLogger converts the current logger to a standard library logger instance.
AsStandardLogger() *stdlog.Logger
// SetStackPrefixFilter sets the call stack prefix filter rules.
SetStackPrefixFilter(...string) Logger
}
// New creates a new Logger instance.
// By default, the logger level is TraceLevel and logs will be output to os.Stdout.
func New(name string) Logger {
return &logger{log{core: newCore(name)}}
}
// The logger type is an implementation of the built-in logger interface.
type logger struct {
log
}
// GetLevel returns the current logger level.
func (o *logger) GetLevel() Level {
return Level(atomic.LoadUint32(&o.core.level))
}
// SetLevel sets the current logger level.
// When the given log level is invalid, this method does nothing.
func (o *logger) SetLevel(level Level) Logger {
if level.IsValid() {
atomic.StoreUint32(&o.core.level, uint32(level))
}
return o
}
// SetLevelString sets the current logger level by string.
func (o *logger) SetLevelString(s string) error {
level, err := ParseLevel(s)
if err != nil {
return err
}
o.SetLevel(level)
return nil
}
// ForceSetLevelString sets the current logger level by string.
// When the given log level string is invalid, this method does nothing.
func (o *logger) ForceSetLevelString(s string) Logger {
if level, err := ParseLevel(s); err == nil {
o.SetLevel(level)
}
return o
}
// SetOutput sets the current logger output writer.
// If the given writer is nil, os.Stdout is used.
func (o *logger) SetOutput(w io.Writer) Logger {
if w == nil {
o.core.writer = os.Stdout
} else {
o.core.writer = w
}
return o
}
// SetLevelOutput sets the current logger level output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the level writer will be disabled.
func (o *logger) SetLevelOutput(level Level, w io.Writer) Logger {
if w == nil {
delete(o.core.levelWriter, level)
} else {
o.core.levelWriter[level] = w
}
return o
}
// SetLevelsOutput sets the current logger levels output writer.
// The level output writer is used to write log data of a given level.
// If the given writer is nil, the levels writer will be disabled.
func (o *logger) SetLevelsOutput(levels []Level, w io.Writer) Logger {
for i, j := 0, len(levels); i < j; i++ {
o.SetLevelOutput(levels[i], w)
}
return o
}
// SetOutputInterceptor sets the output interceptor for the current logger.
// If the given interceptor is nil, the log data is written to the output writer.
func (o *logger) SetOutputInterceptor(f func(Summary, io.Writer) (int, error)) Logger {
o.core.interceptor = f
return o
}
// SetNowFunc sets the function that gets the current time.
// If the given function is nil, time.Now is used.
func (o *logger) SetNowFunc(f func() time.Time) Logger {
if f == nil {
o.core.nowFunc = internal.DefaultNowFunc
} else {
o.core.nowFunc = f
}
return o
}
// SetExitFunc sets the exit function of the current logger.
// If the given function is nil, the exit function is disabled.
// The exit function is called automatically after the FatalLevel level log is recorded.
// By default, the exit function we use is os.Exit.
func (o *logger) SetExitFunc(f func(int)) Logger {
if f == nil {
o.core.exitFunc = internal.EmptyExitFunc
} else {
o.core.exitFunc = f
}
return o
}
// SetPanicFunc sets the panic function of the current logger.
// If the given function is nil, the panic function is disabled.
// The panic function is called automatically after the PanicLevel level log is recorded.
// By default, the panic function we use is func(s string) { panic(s) }.
func (o *logger) SetPanicFunc(f func(string)) Logger {
if f == nil {
o.core.panicFunc = internal.EmptyPanicFunc
} else {
o.core.panicFunc = f
}
return o
}
// SetFormatter sets the log formatter for the current logger.
// If the given log formatter is nil, we will record the log in JSON format.
func (o *logger) SetFormatter(formatter Formatter) Logger {
if formatter == nil {
o.core.formatter = DefaultJSONFormatter()
} else {
o.core.formatter = formatter
}
return o
}
// SetFormatOutput sets the log format output.
// After setting the format output, the format and output of the logger will be controlled by this structure,
// and the bound log output and log level output will no longer be used.
// If format output needs to be disabled, set to nil and the logger will back to the original behavior.
func (o *logger) SetFormatOutput(fo FormatOutput) Logger {
o.core.formatOutput = fo
return o
}
// SetDefaultTimeFormat sets the default log time format for the current logger.
// If the given time format is empty string, internal.DefaultTimeFormat is used.
func (o *logger) SetDefaultTimeFormat(format string) Logger {
if format == "" {
o.core.timeFormat = internal.DefaultTimeFormat
} else {
o.core.timeFormat = format
}
return o
}
// EnableCaller enables caller reporting on all levels of logs.
func (o *logger) EnableCaller(skip ...int) Logger {
var n int
if len(skip) > 0 && skip[0] > 0 {
n = skip[0]
}
o.core.caller = internal.NewCallerReporter(n)
return o
}
// EnableLevelCaller enables caller reporting on logs of a given level.
func (o *logger) EnableLevelCaller(level Level, skip ...int) Logger {
var n int
if len(skip) > 0 && skip[0] > 0 {
n = skip[0]
}
o.core.levelCaller[level] = internal.NewCallerReporter(n)
return o
}
// SetCallerSkip sets a fixed number of skipped callers.
// This method only takes effect for the log with caller enabled.
func (o *logger) SetCallerSkip(skip int) Logger {
if skip < 0 {
skip = 0
}
o.core.callerSkip = skip
return o
}
// SetLongCaller sets whether to enable or disable long caller name (with parent directory name).
func (o *logger) SetLongCaller(long bool) Logger {
o.core.callerLong = long
return o
}
// EnableLevelsCaller enables caller reporting on logs of the given levels.
func (o *logger) EnableLevelsCaller(levels []Level, skip ...int) Logger {
for i, j := 0, len(levels); i < j; i++ {
o.EnableLevelCaller(levels[i], skip...)
}
return o
}
// AddHook adds the given log hook to the current logger.
func (o *logger) AddHook(hook Hook) Logger {
o.core.hooks.Add(hook)
return o
}
// AddHookFunc adds the given log hook function to the current logger.
func (o *logger) AddHookFunc(levels []Level, hook func(Summary) error) Logger {
return o.AddHook(NewHookFromFunc(levels, hook))
}
// EnableHook enables or disables the log hook.
func (o *logger) EnableHook(ok bool) Logger {
o.core.enableHooks = ok
return o
}
// AsLog converts current Logger to Log instances, which is unidirectional.
func (o *logger) AsLog() Log {
return &o.log
}
// AsStandardLogger converts the current logger to a standard library logger instance.
func (o *logger) AsStandardLogger() *stdlog.Logger {
return stdlog.New(NewLevelWriter(InfoLevel, o.AsLog()), "", 0)
}
// SetStackPrefixFilter sets the call stack prefix filter rules.
func (o *logger) SetStackPrefixFilter(prefixes ...string) Logger {
o.core.stackPrefixes = internal.FormatKnownStackPrefixes(prefixes...)
return o
}