-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstd.go
219 lines (187 loc) · 6.17 KB
/
std.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
package log
import (
"fmt"
stdlog "log"
)
// NewStandard sets up a basic logger using the general one provided in the Go
// standard library
func NewStandard() *StdLogger {
return &StdLogger{
Level: InfoLevel,
}
}
const (
// TraceLevel is the constant to use when setting the Trace level for loggers
// provided by this library.
TraceLevel = iota
// DebugLevel is the constant to use when setting the Debug level for loggers
// provided by this library.
DebugLevel
// InfoLevel is the constant to use when setting the Info level for loggers
// provided by this library.
InfoLevel
// WarnLevel is the constant to use when setting the Warn level for loggers
// provided by this library.
WarnLevel
// ErrorLevel is the constant to use when setting the Error level for loggers
// provided by this library.
ErrorLevel
// PanicLevel is the constant to use when setting the Panic level for loggers
// provided by this library.
PanicLevel
// FatalLevel is the constant to use when setting the Fatal level for loggers
// provided by this library.
FatalLevel
)
// StdLogger is a struct that wraps the general logger provided by the Go
// standard library and causes it to meet the log.Logger interface
type StdLogger struct {
Level int
}
// Trace logs a message at the Trace level
func (l StdLogger) Trace(msg ...interface{}) {
if l.Level <= TraceLevel {
stdlog.Print(append([]interface{}{"[TRACE] "}, msg...)...)
}
}
// Tracef formats a message according to a format specifier and logs the
// message at the Trace level
func (l StdLogger) Tracef(template string, args ...interface{}) {
if l.Level <= TraceLevel {
stdlog.Printf("[TRACE] "+template, args...)
}
}
// Tracew logs a message at the Trace level along with some additional
// context (key-value pairs)
func (l StdLogger) Tracew(msg string, fields Fields) {
if l.Level <= TraceLevel {
stdlog.Printf("[TRACE] %s %s", msg, handlFields(fields))
}
}
// Debug logs a message at the Debug level
func (l StdLogger) Debug(msg ...interface{}) {
if l.Level <= DebugLevel {
stdlog.Print(append([]interface{}{"[DEBUG] "}, msg...)...)
}
}
// Debugf formats a message according to a format specifier and logs the
// message at the Debug level
func (l StdLogger) Debugf(template string, args ...interface{}) {
if l.Level <= DebugLevel {
stdlog.Printf("[DEBUG] "+template, args...)
}
}
// Debugw logs a message at the Debug level along with some additional
// context (key-value pairs)
func (l StdLogger) Debugw(msg string, fields Fields) {
if l.Level <= DebugLevel {
stdlog.Printf("[DEBUG] %s %s", msg, handlFields(fields))
}
}
// Info logs a message at the Info level
func (l StdLogger) Info(msg ...interface{}) {
if l.Level <= InfoLevel {
stdlog.Print(append([]interface{}{"[INFO] "}, msg...)...)
}
}
// Infof formats a message according to a format specifier and logs the
// message at the Info level
func (l StdLogger) Infof(template string, args ...interface{}) {
if l.Level <= InfoLevel {
stdlog.Printf("[INFO] "+template, args...)
}
}
// Infow logs a message at the Info level along with some additional
// context (key-value pairs)
func (l StdLogger) Infow(msg string, fields Fields) {
if l.Level <= InfoLevel {
stdlog.Printf("[INFO] %s %s", msg, handlFields(fields))
}
}
// Warn logs a message at the Warn level
func (l StdLogger) Warn(msg ...interface{}) {
if l.Level <= WarnLevel {
stdlog.Print(append([]interface{}{"[WARNING] "}, msg...)...)
}
}
// Warnf formats a message according to a format specifier and logs the
// message at the Warning level
func (l StdLogger) Warnf(template string, args ...interface{}) {
if l.Level <= WarnLevel {
stdlog.Printf("[WARNING] "+template, args...)
}
}
// Warnw logs a message at the Warning level along with some additional
// context (key-value pairs)
func (l StdLogger) Warnw(msg string, fields Fields) {
if l.Level <= WarnLevel {
stdlog.Printf("[WARNING] %s %s", msg, handlFields(fields))
}
}
// Error logs a message at the Error level
func (l StdLogger) Error(msg ...interface{}) {
if l.Level <= ErrorLevel {
stdlog.Print(append([]interface{}{"[ERROR] "}, msg...)...)
}
}
// Errorf formats a message according to a format specifier and logs the
// message at the Error level
func (l StdLogger) Errorf(template string, args ...interface{}) {
if l.Level <= ErrorLevel {
stdlog.Printf("[ERROR] "+template, args...)
}
}
// Errorw logs a message at the Error level along with some additional
// context (key-value pairs)
func (l StdLogger) Errorw(msg string, fields Fields) {
if l.Level <= ErrorLevel {
stdlog.Printf("[ERROR] %s %s", msg, handlFields(fields))
}
}
// Panic logs a message at the Panic level and panics
func (l StdLogger) Panic(msg ...interface{}) {
if l.Level <= PanicLevel {
stdlog.Panic(append([]interface{}{"[PANIC] "}, msg...)...)
}
}
// Panicf formats a message according to a format specifier and logs the
// message at the Panic level and then panics
func (l StdLogger) Panicf(template string, args ...interface{}) {
if l.Level <= PanicLevel {
stdlog.Panicf("[PANIC] "+template, args...)
}
}
// Panicw logs a message at the Panic level along with some additional
// context (key-value pairs) and then panics
func (l StdLogger) Panicw(msg string, fields Fields) {
if l.Level <= PanicLevel {
stdlog.Panicf("[PANIC] %s %s", msg, handlFields(fields))
}
}
// Fatal logs a message at the Fatal level and exists the application
func (l StdLogger) Fatal(msg ...interface{}) {
if l.Level <= FatalLevel {
stdlog.Fatal(append([]interface{}{"[FATAL] "}, msg...)...)
}
}
// Fatalf formats a message according to a format specifier and logs the
// message at the Fatal level and exits the application
func (l StdLogger) Fatalf(template string, args ...interface{}) {
if l.Level <= FatalLevel {
stdlog.Fatalf("[FATAL] "+template, args...)
}
}
// Fatalw logs a message at the Fatal level along with some additional
// context (key-value pairs) and exits the application
func (l StdLogger) Fatalw(msg string, fields Fields) {
if l.Level <= FatalLevel {
stdlog.Fatalf("[FATAL] %s %s", msg, handlFields(fields))
}
}
func handlFields(flds Fields) string {
var ret string
for k, v := range flds {
ret += fmt.Sprintf("[%s=%s]", k, v)
}
return ret
}