-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
502 lines (436 loc) · 10.8 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2021 huija
//
// 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 tao
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strconv"
"sync"
)
// Log config in tao
type Log struct {
Level LogLevel `json:"level"`
Type LogType `json:"type"`
Flag LogFlag `json:"flag"`
CallDepth int `json:"call_depth"`
Path string `json:"path,omitempty"`
Disable bool `json:"disable"`
}
// LogLevel log's level
type LogLevel uint8
const (
// DEBUG (usually) is used in development env to print track info but disabled in production env to avoid overweight logs
DEBUG LogLevel = iota
// INFO (usually) is default level to print some core infos
INFO
// WARNING should be mentioned, it's more important than INFO
WARNING
// ERROR must be solved, program shouldn't generate any error-level logs.
ERROR
// PANIC logs a message, then panics.
PANIC
// FATAL logs a message, then calls os.Exit(1).
FATAL
)
// String for LogLevel Config
func (l LogLevel) String() string {
switch l {
case DEBUG:
return "debug"
case INFO:
return "info"
case WARNING:
return "warning"
case ERROR:
return "error"
case PANIC:
return "panic"
case FATAL:
return "fatal"
default:
return fmt.Sprintf("tao.LogLevel(%d)", l)
}
}
// MarshalText instead of number
func (l LogLevel) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText to number
func (l *LogLevel) UnmarshalText(text []byte) error {
switch lower := string(bytes.ToLower(text)); lower {
case "debug":
*l = DEBUG
case "info":
*l = INFO
case "warning":
*l = WARNING
case "error":
*l = ERROR
case "panic":
*l = PANIC
case "fatal":
*l = FATAL
default:
return fmt.Errorf("log: unrecognized LogLevel: %q", lower)
}
return nil
}
// LogType log's type
type LogType uint8
const (
// Console log
Console LogType = 1 // 0b1
// File log
File LogType = 2 // 0b10
)
// String for LogType Config
func (l LogType) String() string {
switch l {
case Console:
return "console"
case File:
return "file"
case Console | File:
return "console|file"
default:
return fmt.Sprintf("tao.LOGTYPE(%d)", l)
}
}
// MarshalText instead of number
func (l LogType) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText to number
func (l *LogType) UnmarshalText(text []byte) error {
switch lower := string(bytes.ToLower(text)); lower {
case "console":
*l = Console
case "file":
*l = File
case "console|file", "file|console":
*l = File | Console
default:
return fmt.Errorf("log: unrecognized LogType: %q", lower)
}
return nil
}
// LogFlag log's flag
type LogFlag int
// String for LogType Config
func (l LogFlag) String() string {
switch l {
case log.LstdFlags:
return "std"
case log.LstdFlags | log.Lshortfile:
return "std|short"
case log.LstdFlags | log.Llongfile:
return "std|long"
default:
return fmt.Sprintf("tao.LogFlag(%d)", l)
}
}
// MarshalText instead of number
func (l LogFlag) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText to number
func (l *LogFlag) UnmarshalText(text []byte) error {
switch lower := string(bytes.ToLower(text)); lower {
case "std":
*l = log.LstdFlags
case "std|short", "short|std":
*l = log.LstdFlags | log.Lshortfile
case "std|long", "long|std":
*l = log.LstdFlags | log.Llongfile
default:
// int flag
flag, err := strconv.Atoi(lower)
if err != nil {
return fmt.Errorf("log: unrecognized LogFlag: %q", lower)
}
*l = LogFlag(flag)
}
return nil
}
// Logger in tao
type Logger interface {
Debug(v ...interface{})
Debugf(format string, v ...interface{})
Info(v ...interface{})
Infof(format string, v ...interface{})
Warn(v ...interface{})
Warnf(format string, v ...interface{})
Error(v ...interface{})
Errorf(format string, v ...interface{})
Panic(v ...interface{})
Panicf(format string, v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
}
var _ Logger = (*logger)(nil)
// logger implements Logger using standard lib
type logger struct {
*log.Logger
calldepth int
}
// levelPrefix to define log prefix of log level
var levelPrefix = map[LogLevel]string{
DEBUG: "[D] ",
INFO: "[I] ",
WARNING: "[W] ",
ERROR: "[E] ",
PANIC: "[P] ",
FATAL: "[F] ",
}
// Debug logs info in debug level
func (l *logger) Debug(v ...interface{}) {
if t.Log.Level > DEBUG {
return
}
_ = l.Output(l.calldepth, levelPrefix[DEBUG]+fmt.Sprintln(v...))
}
// Debugf logs info in debug level
func (l *logger) Debugf(format string, v ...interface{}) {
if t.Log.Level > DEBUG {
return
}
_ = l.Output(l.calldepth, levelPrefix[DEBUG]+fmt.Sprintf(format, v...))
}
// Info logs info in info level
func (l *logger) Info(v ...interface{}) {
if t.Log.Level > INFO {
return
}
_ = l.Output(l.calldepth, levelPrefix[INFO]+fmt.Sprintln(v...))
}
// Infof logs info in info level
func (l *logger) Infof(format string, v ...interface{}) {
if t.Log.Level > INFO {
return
}
_ = l.Output(l.calldepth, levelPrefix[INFO]+fmt.Sprintf(format, v...))
}
// Warn logs info in warn level
func (l *logger) Warn(v ...interface{}) {
if t.Log.Level > WARNING {
return
}
_ = l.Output(l.calldepth, levelPrefix[WARNING]+fmt.Sprintln(v...))
}
// Warnf logs info in warn level
func (l *logger) Warnf(format string, v ...interface{}) {
if t.Log.Level > WARNING {
return
}
_ = l.Output(l.calldepth, levelPrefix[WARNING]+fmt.Sprintf(format, v...))
}
// Error logs info in error level
func (l *logger) Error(v ...interface{}) {
if t.Log.Level > ERROR {
return
}
_ = l.Output(l.calldepth, levelPrefix[ERROR]+fmt.Sprintln(v...))
}
// Errorf logs info in error level
func (l *logger) Errorf(format string, v ...interface{}) {
if t.Log.Level > ERROR {
return
}
_ = l.Output(l.calldepth, levelPrefix[ERROR]+fmt.Sprintf(format, v...))
}
// Panic logs info in panic level
func (l *logger) Panic(v ...interface{}) {
if t.Log.Level > PANIC {
return
}
s := levelPrefix[PANIC] + fmt.Sprintln(v...)
_ = l.Output(l.calldepth, s)
panic(s)
}
// Panicf logs info in panic level
func (l *logger) Panicf(format string, v ...interface{}) {
if t.Log.Level > PANIC {
return
}
s := levelPrefix[PANIC] + fmt.Sprintf(format, v...)
_ = l.Output(l.calldepth, s)
panic(s)
}
// Fatal logs info in fatal level
func (l *logger) Fatal(v ...interface{}) {
if t.Log.Level > FATAL {
return
}
_ = l.Output(l.calldepth, levelPrefix[FATAL]+fmt.Sprintln(v...))
os.Exit(1)
}
// Fatalf logs info in fatal level
func (l *logger) Fatalf(format string, v ...interface{}) {
if t.Log.Level > FATAL {
return
}
_ = l.Output(l.calldepth, levelPrefix[FATAL]+fmt.Sprintf(format, v...))
os.Exit(1)
}
// Close this logger
func (l *logger) Close() error {
return nil
}
// taoLogger for tao
type taoLogger struct {
mu sync.Mutex
loggers map[string]Logger
writers map[string]io.Writer
}
// globalLogger which default to provide based log print
var globalLogger = new(taoLogger)
// GetWriter in tao
func GetWriter(configKey string) io.Writer {
return globalLogger.writers[configKey]
}
// SetWriter to tao
func SetWriter(configKey string, w io.Writer) error {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
if globalLogger.writers == nil {
globalLogger.writers = make(map[string]io.Writer)
}
if _, ok := globalLogger.writers[configKey]; ok {
return NewError(DuplicateCall, "log: %s's writer has been set before", configKey)
}
globalLogger.writers[configKey] = w
return nil
}
// DeleteWriter of tao
func DeleteWriter(configKey string) error {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
writer, ok := globalLogger.writers[configKey]
if !ok {
return NewError(ParamInvalid, "log: %s's writer not set", configKey)
}
delete(globalLogger.writers, configKey)
// writer close
if l, ok := writer.(io.Closer); ok {
return l.Close()
}
return nil
}
// GetLogger in tao
func GetLogger(configKey string) Logger {
return globalLogger.loggers[configKey]
}
// SetLogger to tao
func SetLogger(configKey string, logger Logger) error {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
if globalLogger.loggers == nil {
globalLogger.loggers = make(map[string]Logger)
}
if _, ok := globalLogger.loggers[configKey]; ok {
return NewError(DuplicateCall, "log: %s's logger has been set before", configKey)
}
globalLogger.loggers[configKey] = logger
return nil
}
// DeleteLogger of tao
func DeleteLogger(configKey string) error {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
logger, ok := globalLogger.loggers[configKey]
if !ok {
return NewError(ParamInvalid, "log: %s's logger not set", configKey)
}
delete(globalLogger.loggers, configKey)
// logger close
if l, ok := logger.(io.Closer); ok {
return l.Close()
}
return nil
}
// Debug function wrap of taoLogger
func Debug(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Debug(v...)
}
}
// Debugf function wrap of taoLogger
func Debugf(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Debugf(format, v...)
}
}
// Info function wrap of taoLogger
func Info(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Info(v...)
}
}
// Infof function wrap of taoLogger
func Infof(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Infof(format, v...)
}
}
// Warn function wrap of taoLogger
func Warn(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Warn(v...)
}
}
// Warnf function wrap of taoLogger
func Warnf(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Warnf(format, v...)
}
}
// Error function wrap of taoLogger
func Error(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Error(v...)
}
}
// Errorf function wrap of taoLogger
func Errorf(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Errorf(format, v...)
}
}
// Panic function wrap of taoLogger
func Panic(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Panic(v...)
}
}
// Panicf function wrap of taoLogger
func Panicf(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Panicf(format, v...)
}
}
// Fatal function wrap of taoLogger
func Fatal(v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Fatal(v...)
}
}
// Fatalf function wrap of taoLogger
func Fatalf(format string, v ...interface{}) {
for _, l := range globalLogger.loggers {
l.Fatalf(format, v...)
}
}