-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
470 lines (405 loc) · 14.4 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package log
import (
"io"
"strings"
"sync"
"github.com/goloop/g"
)
var (
self *Logger // is the default logger instance
mu sync.Mutex
)
// Log returns the default logger instance.
func Log() *Logger {
return self
}
// New returns a new Logger object with optional prefixes for log messages.
// If one or more prefixes are provided, they will be concatenated with
// hyphens and prepended to each log message. Leading and trailing whitespace
// characters are stripped from each prefix before concatenation.
//
// Example usage:
//
// // Create a logger without any prefixes.
// logger := log.New()
// logger.Info("Hello, World!")
//
// // Create a logger with a single prefix 'MYAPP'.
// loggerWithPrefix := log.New("MYAPP")
// loggerWithPrefix.Info("Hello, World!")
//
// // Create a logger with multiple prefixes 'MYAPP' and 'PREFIX'.
// // The prefixes will be joined as 'MYAPP-PREFIX'.
// loggerWithMultiplePrefixes := log.New("MYAPP", "PREFIX")
// loggerWithMultiplePrefixes.Info("Hello, World!")
//
// // Create a logger with prefixes containing leading/trailing whitespace.
// // The whitespace is removed and prefixes are joined as 'MYAPP-PREFIX'.
// loggerWithWhitespace := log.New(" MYAPP ", " PREFIX ")
// loggerWithWhitespace.Info("Hello, World!")
//
// Parameters:
//
// prefixes: Optional string values to prepend to each log message.
// If multiple prefixes are provided, they will be concatenated
// with hyphens.
//
// Returns:
//
// A new Logger instance configured with the provided prefixes.
func New(prefixes ...string) *Logger {
// Generate prefix.
prefix := ""
if len(prefixes) != 0 {
// Concatenate prefixes.
if l := len(prefixes); l == 1 {
// If there is only one prefix, use it as is.
// In this case, no changes are made to the prefix.
prefix = prefixes[0]
} else if l > 1 {
// Several words that characterize the prefix are given.
// In this case, they must be combined as ONE-TWO-THREE in
// upper case, removing all special characters such as spaces,
// colons and \t, \i, \n.
i := 0
sb := strings.Builder{}
for _, p := range prefixes {
v := g.Trim(p, " \t\n\r")
if v == "" {
continue
}
// If one character is installed, it is added without
// the separator '-' and is considered a marker of the
// end of the prefix.
// {"MYAPP:"} => "MYAPP:"
// {"MYAPP", ":"} => "MYAPP:"
// {"MY", "APP", ":"} => "MY-APP:"
if l := len(v); i != 0 && l > 1 {
sb.WriteString("-")
}
i++
sb.WriteString(v)
}
// If the prefix is not empty, add a marker at the end.
if sb.Len() > 0 {
prefix = sb.String()
}
}
}
logger := &Logger{
skipStackFrames: skipStackFrames,
fatalStatusCode: fatalStatusCode,
prefix: prefix,
outputs: map[string]*Output{},
}
logger.SetOutputs(Stdout, Stderr)
return logger
}
// InitializeDefaultLogger initializes the default logger instance.
// The function is called when the module is initialized.
// Calling the function again has no result.
func InitializeDefaultLogger() {
mu.Lock()
defer mu.Unlock()
if self == nil {
self = New()
skip := skipStackFrames + 1 // self works at the imported package level
self.SetSkipStackFrames(skip)
}
}
// Initializes the logger.
func init() {
InitializeDefaultLogger()
}
// Copy returns copy of the log object.
func Copy() *Logger {
return self.Copy()
}
// SetSkipStackFrames sets skip stack frames level.
func SetSkipStackFrames(skips int) {
self.SetSkipStackFrames(skips)
}
// SkipStackFrames returns skip stack frames level.
func SkipStackFrames() int {
return self.SkipStackFrames()
}
// SetPrefix sets the name of the logger object.
func SetPrefix(prefix string) string {
return self.SetPrefix(prefix)
}
// Prefix returns the name of the log object.
func Prefix() string {
return self.Prefix()
}
// SetOutputs sets the outputs of the log object.
func SetOutputs(outputs ...Output) error {
return self.SetOutputs(outputs...)
}
// EditOutputs edits the outputs of the log object.
func EditOutputs(outputs ...Output) error {
return self.EditOutputs(outputs...)
}
// DeleteOutputs deletes the outputs of the log object.
func DeleteOutputs(names ...string) {
self.DeleteOutputs(names...)
}
// Outputs returns a list of outputs.
func Outputs(names ...string) []Output {
return self.Outputs(names...)
}
// Fpanic creates message with Panic level, using the default formats
// for its operands and writes to w. Spaces are added between operands
// when neither is a string.
func Fpanic(w io.Writer, a ...any) {
self.Fpanic(w, a...)
}
// Fpanicf creates message with Panic level, according to a format
// specifier and writes to w.
func Fpanicf(w io.Writer, format string, a ...any) {
self.Fpanicf(w, format, a...)
}
// Fpanicln creates message with Panic level, using the default formats
// for its operands and writes to w. Spaces are always added between
// operands and a newline is appended.
func Fpanicln(w io.Writer, a ...any) {
self.Fpanicln(w, a...)
}
// Panic creates message with Panic level, using the default formats
// for its operands and writes to log.Writer. Spaces are added between
// operands when neither is a string.
func Panic(a ...any) {
self.Panic(a...)
}
// Panicf creates message with Panic level, according to a format specifier
// and writes to log.Writer.
func Panicf(format string, a ...any) {
self.Panicf(format, a...)
}
// Panicln creates message with Panic, level using the default formats
// for its operands and writes to log.Writer. Spaces are always added
// between operands and a newline is appended.
func Panicln(a ...any) {
self.Panicln(a...)
}
// 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.
func Ffatal(w io.Writer, a ...any) {
self.Ffatal(w, a...)
}
// Ffatalf creates message with Fatal level, according to a format
// specifier and writes to w.
func Ffatalf(w io.Writer, format string, a ...any) {
self.Ffatalf(w, 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 Ffatalln(w io.Writer, a ...any) {
self.Ffatalln(w, 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 Fatal(a ...any) {
self.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 Fatalf(format string, a ...any) {
self.Fatalf(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.
func Fatalln(a ...any) {
self.Fatalln(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.
func Ferror(w io.Writer, a ...any) {
self.Ferror(w, a...)
}
// Ferrorf creates message with Error level, according to a format
// specifier and writes to w.
func Ferrorf(w io.Writer, format string, a ...any) {
self.Ferrorf(w, 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 Ferrorln(w io.Writer, a ...any) {
self.Ferrorln(w, 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 Error(a ...any) {
self.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 Errorf(format string, a ...any) {
self.Errorf(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.
func Errorln(a ...any) {
self.Errorln(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.
func Fwarn(w io.Writer, a ...any) {
self.Fwarn(w, a...)
}
// Fwarnf creates message with Warn level, according to a format
// specifier and writes to w.
func Fwarnf(w io.Writer, format string, a ...any) {
self.Fwarnf(w, 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 Fwarnln(w io.Writer, a ...any) {
self.Fwarnln(w, 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 Warn(a ...any) {
self.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 Warnf(format string, a ...any) {
self.Warnf(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.
func Warnln(a ...any) {
self.Warnln(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.
func Finfo(w io.Writer, a ...any) {
self.Finfo(w, a...)
}
// Finfof creates message with Info level, according to a format
// specifier and writes to w.
func Finfof(w io.Writer, format string, a ...any) {
self.Finfof(w, 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 Finfoln(w io.Writer, a ...any) {
self.Finfoln(w, 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 Info(a ...any) {
self.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 Infof(format string, a ...any) {
self.Infof(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.
func Infoln(a ...any) {
self.Infoln(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.
func Fdebug(w io.Writer, a ...any) {
self.Fdebug(w, a...)
}
// Fdebugf creates message with Debug level, according to a format
// specifier and writes to w.
func Fdebugf(w io.Writer, format string, a ...any) {
self.Fdebugf(w, 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 Fdebugln(w io.Writer, a ...any) {
self.Fdebugln(w, 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 Debug(a ...any) {
self.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 Debugf(format string, a ...any) {
self.Debugf(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.
func Debugln(a ...any) {
self.Debugln(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.
func Ftrace(w io.Writer, a ...any) {
self.Ftrace(w, a...)
}
// Ftracef creates message with Trace level, according to a format
// specifier and writes to w.
func Ftracef(w io.Writer, format string, a ...any) {
self.Ftracef(w, 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 Ftraceln(w io.Writer, a ...any) {
self.Ftraceln(w, 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 Trace(a ...any) {
self.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 Tracef(format string, a ...any) {
self.Tracef(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.
func Traceln(a ...any) {
self.Traceln(a...)
}