/
logger.go
177 lines (135 loc) · 4.98 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
package log
import (
"os"
"github.com/tableauio/tableau/log/core"
"github.com/tableauio/tableau/log/driver"
)
type LoggerIface interface {
// Debug uses fmt.Sprint to construct and log a message.
Debug(args ...interface{})
// Info uses fmt.Sprint to construct and log a message.
Info(args ...interface{})
// Warn uses fmt.Sprint to construct and log a message.
Warn(args ...interface{})
// Error uses fmt.Sprint to construct and log a message.
Error(args ...interface{})
// DPanic uses fmt.Sprint to construct and log a message. In development, the
// logger then panics. (See DPanicLevel for details.)
DPanic(args ...interface{})
// Panic uses fmt.Sprint to construct and log a message, then panics.
Panic(args ...interface{})
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
Fatal(args ...interface{})
// Debugf uses fmt.Sprintf to log a templated message.
Debugf(format string, args ...interface{})
// Infof uses fmt.Sprintf to log a templated message.
Infof(format string, args ...interface{})
// Warnf uses fmt.Sprintf to log a templated message.
Warnf(format string, args ...interface{})
// Errorf uses fmt.Sprintf to log a templated message.
Errorf(format string, args ...interface{})
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
// logger then panics. (See DPanicLevel for details.)
DPanicf(format string, args ...interface{})
// Panicf uses fmt.Sprintf to log a templated message, then panics.
Panicf(format string, args ...interface{})
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
Fatalf(format string, args ...interface{})
}
type Logger struct {
level core.Level
driver driver.Driver
}
func (l *Logger) Debug(args ...interface{}) {
l.log(core.DebugLevel, "", args, nil)
}
func (l *Logger) Info(args ...interface{}) {
l.log(core.InfoLevel, "", args, nil)
}
func (l *Logger) Warn(args ...interface{}) {
l.log(core.WarnLevel, "", args, nil)
}
func (l *Logger) Error(args ...interface{}) {
l.log(core.ErrorLevel, "", args, nil)
}
func (l *Logger) DPanic(args ...interface{}) {
l.log(core.DPanicLevel, "", args, nil)
// TODO: panic only in development
panic("log panic")
}
func (l *Logger) Panic(args ...interface{}) {
l.log(core.PanicLevel, "", args, nil)
panic("log panic")
}
func (l *Logger) Fatal(args ...interface{}) {
l.log(core.FatalLevel, "", args, nil)
os.Exit(-1)
}
func (l *Logger) Debugf(format string, args ...interface{}) {
l.log(core.DebugLevel, format, args, nil)
}
func (l *Logger) Infof(format string, args ...interface{}) {
l.log(core.InfoLevel, format, args, nil)
}
func (l *Logger) Warnf(format string, args ...interface{}) {
l.log(core.WarnLevel, format, args, nil)
}
func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(core.ErrorLevel, format, args, nil)
}
func (l *Logger) DPanicf(format string, args ...interface{}) {
l.log(core.DPanicLevel, format, args, nil)
// TODO: panic only in development
panic("log panic")
}
func (l *Logger) Panicf(format string, args ...interface{}) {
l.log(core.PanicLevel, format, args, nil)
panic("log panic")
}
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log(core.FatalLevel, format, args, nil)
os.Exit(-1)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) {
l.log(core.DebugLevel, msg, nil, keysAndValues)
}
// Infow logs a message with some additional context.
func (l *Logger) Infow(msg string, keysAndValues ...interface{}) {
l.log(core.InfoLevel, msg, nil, keysAndValues)
}
// Warnw logs a message with some additional context.
func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) {
l.log(core.WarnLevel, msg, nil, keysAndValues)
}
// Errorw logs a message with some additional context.
func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) {
l.log(core.ErrorLevel, msg, nil, keysAndValues)
}
// DPanicw logs a message with some additional context.
func (l *Logger) DPanicw(msg string, keysAndValues ...interface{}) {
l.log(core.DPanicLevel, msg, nil, keysAndValues)
}
// Panicw logs a message with some additional context.
func (l *Logger) Panicw(msg string, keysAndValues ...interface{}) {
l.log(core.PanicLevel, msg, nil, keysAndValues)
}
// Fatalw logs a message with some additional context.
func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) {
l.log(core.FatalLevel, msg, nil, keysAndValues)
}
func (l *Logger) log(lvl core.Level, format string, fmtArgs []interface{}, kvs []interface{}) {
// If logging at this level is completely disabled, skip the overhead of
// string formatting.
// if lvl < DPanicLevel {
// return
// }
r := &core.Record{
Level: lvl,
Format: &format,
Args: fmtArgs,
KVs: kvs,
}
l.driver.Print(r)
}