-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
419 lines (357 loc) · 15.7 KB
/
log.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package log
import (
"fmt"
"io"
"os"
)
// Timestamp is the format for displaying the time stamp in the log message.
const Timestamp = "01.02.2006 15:04:05"
// Log this is the logging object.
type Log struct {
// Writer is the message receiver object (os.Stdout by default).
Writer io.Writer
// Timestamp is the format for displaying the
// time stamp in the log message.
Timestamp string
// Levels map of available levels.
Levels Levels
// ShowFilePath if true appends the full path to the go-file,
// the logging method was called.
ShowFilePath bool
// ShowFuncName if true, appends the function name where the
// logging method was called.
ShowFuncName bool
// ShowFileLine if true appends the line number in the go-file
// where the logging method was called.
ShowFileLine bool
}
// New returns new Log object.
// Takes zero or more log levels as arguments. If logging levels are not
// specified, all possible logging levels will be activated, otherwise
// only the specified logging levels will be activated.
func New(levels ...Level) (*Log, error) {
var log = Log{
Writer: os.Stdout,
Timestamp: Timestamp,
Levels: Levels{},
ShowFilePath: false,
ShowFuncName: true,
ShowFileLine: true,
}
if len(levels) > 0 {
log.Levels.Set(levels...)
} else {
log.Levels.Set(FATAL, ERROR, WARN, INFO, DEBUG, TRACE)
}
return &log, nil
}
// The echo method creates a message of the fmt.Fprint format.
// It is used as a base for all levels of logging in the fmt.Fprint format.
func (l *Log) echo(skip int, w io.Writer, level Level,
a ...interface{}) (n int, err error) {
var trace = getTrace(skip)
// If the level is not supported.
if v := l.Levels[level]; !v {
return 0, nil
}
// Generate log prefix.
prefix := getPrefix(trace, "", l.Timestamp, level,
l.ShowFilePath, l.ShowFuncName, l.ShowFileLine)
a = append([]interface{}{prefix}, a...)
return fmt.Fprint(w, a...)
}
// The echof method creates a message of the fmt.Fprintf format.
// It is used as a base for all levels of logging in the fmt.Fprintf format.
func (l *Log) echof(skip int, w io.Writer, level Level, format string,
a ...interface{}) (n int, err error) {
var trace = getTrace(skip)
// If the level is not supported.
if v := l.Levels[level]; !v {
return 0, nil
}
// Generate log prefix.
prefix := getPrefix(trace, format, l.Timestamp, level,
l.ShowFilePath, l.ShowFuncName, l.ShowFileLine)
return fmt.Fprintf(w, prefix, a...)
}
// The echoln method creates a message of the fmt.Fprintln format.
// It is used as a base for all levels of logging in the fmt.Fprintln format.
func (l *Log) echoln(skip int, w io.Writer, level Level,
a ...interface{}) (n int, err error) {
var trace = getTrace(skip)
// If the level is not supported.
if v := l.Levels[level]; !v {
return 0, nil
}
// Generate log prefix.
prefix := getPrefix(trace, "", l.Timestamp, level,
l.ShowFilePath, l.ShowFuncName, l.ShowFileLine)
prefix = prefix[:len(prefix)-1] // remove trailing space
a = append([]interface{}{prefix}, a...)
return fmt.Fprintln(w, a...)
}
// Copy returns copy of the log object.
func (l *Log) Copy() *Log {
var log = Log{
Writer: l.Writer,
Timestamp: l.Timestamp,
Levels: l.Levels,
ShowFilePath: l.ShowFilePath,
ShowFuncName: l.ShowFuncName,
ShowFileLine: l.ShowFileLine,
}
return &log
}
// Format sets the message prefix display configuration flags for display:
// file path, function name and file line.
func (l *Log) Format(showFilePath, showFuncName, showFileLine bool) {
l.ShowFilePath = showFilePath
l.ShowFuncName = showFuncName
l.ShowFileLine = showFileLine
}
// Ffatal creates message with FATAL level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ffatal(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, FATAL, a...)
}
// Ffatalf creates message with FATAL level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ffatalf(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, FATAL, format, a...)
}
// Ffatalln creates message with FATAL level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Ffatalln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, FATAL, a...)
}
// Fatal creates message with FATAL level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Fatal(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, FATAL, a...)
}
// Fatalf creates message with FATAL level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Fatalf(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, FATAL, format, a...)
}
// Fatalln creates message with FATAL, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Fatalln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, FATAL, a...)
}
// Ferror creates message with ERROR level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ferror(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, ERROR, a...)
}
// Ferrorf creates message with ERROR level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ferrorf(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, ERROR, format, a...)
}
// Ferrorln creates message with ERROR level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Ferrorln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, ERROR, a...)
}
// Error creates message with ERROR level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Error(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, ERROR, a...)
}
// Errorf creates message with ERROR level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Errorf(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, ERROR, format, a...)
}
// Errorln creates message with ERROR, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Errorln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, ERROR, a...)
}
// Fwarn creates message with WARN level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Fwarn(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, WARN, a...)
}
// Fwarnf creates message with WARN level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Fwarnf(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, WARN, format, a...)
}
// Fwarnln creates message with WARN level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Fwarnln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, WARN, a...)
}
// Warn creates message with WARN level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Warn(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, WARN, a...)
}
// Warnf creates message with WARN level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Warnf(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, WARN, format, a...)
}
// Warnln creates message with WARN, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Warnln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, WARN, a...)
}
// Finfo creates message with INFO level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Finfo(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, INFO, a...)
}
// Finfof creates message with INFO level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Finfof(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, INFO, format, a...)
}
// Finfoln creates message with INFO level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Finfoln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, INFO, a...)
}
// Info creates message with INFO level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Info(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, INFO, a...)
}
// Infof creates message with INFO level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Infof(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, INFO, format, a...)
}
// Infoln creates message with INFO, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Infoln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, INFO, a...)
}
// Fdebug creates message with DEBUG level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Fdebug(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, DEBUG, a...)
}
// Fdebugf creates message with DEBUG level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Fdebugf(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, DEBUG, format, a...)
}
// Fdebugln creates message with DEBUG level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Fdebugln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, DEBUG, a...)
}
// Debug creates message with DEBUG level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Debug(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, DEBUG, a...)
}
// Debugf creates message with DEBUG level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Debugf(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, DEBUG, format, a...)
}
// Debugln creates message with DEBUG, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Debugln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, DEBUG, a...)
}
// Ftrace creates message with TRACE level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ftrace(w io.Writer, a ...interface{}) (n int, err error) {
return l.echo(SKIP, w, TRACE, a...)
}
// Ftracef creates message with TRACE level, according to a format
// specifier and writes to w. It returns the number of bytes written
// and any write error encountered.
func (l *Log) Ftracef(w io.Writer, format string,
a ...interface{}) (n int, err error) {
return l.echof(SKIP, w, TRACE, format, a...)
}
// Ftraceln creates message with TRACE level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Ftraceln(w io.Writer, a ...interface{}) (n int, err error) {
return l.echoln(SKIP, w, TRACE, a...)
}
// Trace creates message with TRACE level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string. It returns the number of bytes
// written and any write error encountered.
func (l *Log) Trace(a ...interface{}) (n int, err error) {
return l.echo(SKIP, l.Writer, TRACE, a...)
}
// Tracef creates message with TRACE level, according to a format specifier
// and writes to log.Writer. It returns the number of bytes written and any
// write error encountered.
func (l *Log) Tracef(format string, a ...interface{}) (n int, err error) {
return l.echof(SKIP, l.Writer, TRACE, format, a...)
}
// Traceln creates message with TRACE, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended. It returns the number
// of bytes written and any write error encountered.
func (l *Log) Traceln(a ...interface{}) (n int, err error) {
return l.echoln(SKIP, l.Writer, TRACE, a...)
}