-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_logger.go
97 lines (78 loc) · 1.79 KB
/
std_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
package logger
import (
"fmt"
"log"
"os"
)
func NewStdLogger(opts ...Option) Logger {
conf := newConfig()
for _, o := range opts {
o(conf)
}
return &stdLogger{
l: log.New(os.Stderr, conf.Module, log.LstdFlags|log.Lshortfile),
c: conf,
callDepth: 3,
}
}
type stdLogger struct {
l *log.Logger
c *LogConfig
callDepth int
}
func (l *stdLogger) output(lvl Level, msg string) {
if l.c.LogLevel > lvl {
return
}
s := ""
if l.c.ShowColor {
s = l.pack(colorMapping[lvl], msg)
} else {
s = l.pack(mapping[lvl], msg)
}
err := l.l.Output(l.callDepth, s)
if err != nil {
panic(err)
}
}
func (l *stdLogger) pack(lvl, msg string) string {
return fmt.Sprintf("[%s] %s", lvl, msg)
}
func (l *stdLogger) Debug(v ...interface{}) {
l.output(DEBUG, fmt.Sprint(v...))
}
func (l *stdLogger) Debugf(format string, v ...interface{}) {
l.output(DEBUG, fmt.Sprintf(format, v...))
}
func (l *stdLogger) Info(v ...interface{}) {
l.output(INFO, fmt.Sprint(v...))
}
func (l *stdLogger) Infof(format string, v ...interface{}) {
l.output(INFO, fmt.Sprintf(format, v...))
}
func (l *stdLogger) Warn(v ...interface{}) {
l.output(WARN, fmt.Sprint(v...))
}
func (l *stdLogger) Warnf(format string, v ...interface{}) {
l.output(WARN, fmt.Sprintf(format, v...))
}
func (l *stdLogger) Error(v ...interface{}) {
l.output(ERROR, fmt.Sprint(v...))
}
func (l *stdLogger) Errorf(format string, v ...interface{}) {
l.output(ERROR, fmt.Sprintf(format, v...))
}
func (l *stdLogger) Fatal(v ...interface{}) {
l.output(FATAL, fmt.Sprint(v...))
os.Exit(1)
}
func (l *stdLogger) Fatalf(format string, v ...interface{}) {
l.output(FATAL, fmt.Sprintf(format, v...))
os.Exit(1)
}
func (l *stdLogger) SetLevel(lvl Level) {
l.c.LogLevel = lvl
}
func (l *stdLogger) Stop() {
// do nothing
}