forked from rantav/go-logger
-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
264 lines (206 loc) · 6.99 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
package log
import (
"fmt"
stdlog "log"
"os"
)
//go:generate counterfeiter -o shims/fake/fake_logger.go . Logger
// Logger interface allows you to maintain a unified interface while using a
// custom logger. This allows you to write log statements without dictating
// the specific underlying library used for logging. You can avoid vendoring
// of logging libraries, which is especially useful when writing shared code
// such as a library. This package contains a simple logger and a no-op logger
// which both implement this interface. It is also supplemented with some
// additional helpers/shims for other common logging libraries such as logrus
type Logger interface {
Debug(msg ...interface{})
Info(msg ...interface{})
Warn(msg ...interface{})
Error(msg ...interface{})
Fatal(msg ...interface{})
Panic(msg ...interface{})
Debugln(msg ...interface{})
Infoln(msg ...interface{})
Warnln(msg ...interface{})
Errorln(msg ...interface{})
Fatalln(msg ...interface{})
Panicln(msg ...interface{})
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Panicf(format string, args ...interface{})
WithFields(Fields) Logger
}
// Fields is used to define structured fields which are appended to log messages
type Fields map[string]interface{}
/**************
Simple Logger
**************/
type simple struct {
fields map[string]interface{}
}
// NewSimple creates a basic logger that wraps the core log library.
func NewSimple() Logger {
return &simple{}
}
// WithFields will return a new logger based on the original logger
// with the additional supplied fields
func (b *simple) WithFields(fields Fields) Logger {
cp := &simple{}
if b.fields == nil {
cp.fields = fields
return cp
}
cp.fields = make(map[string]interface{}, len(b.fields)+len(fields))
for k, v := range b.fields {
cp.fields[k] = v
}
for k, v := range fields {
cp.fields[k] = v
}
return cp
}
// Debug log message
func (b *simple) Debug(msg ...interface{}) {
stdlog.Printf("[DEBUG] %s %s", fmt.Sprint(msg...), pretty(b.fields))
}
// Info log message
func (b *simple) Info(msg ...interface{}) {
stdlog.Printf("[INFO] %s %s", fmt.Sprint(msg...), pretty(b.fields))
}
// Warn log message
func (b *simple) Warn(msg ...interface{}) {
stdlog.Printf("[WARN] %s %s", fmt.Sprint(msg...), pretty(b.fields))
}
// Error log message
func (b *simple) Error(msg ...interface{}) {
stdlog.Printf("[ERROR] %s %s", fmt.Sprint(msg...), pretty(b.fields))
}
// Fatal log message (and exit)
func (b *simple) Fatal(msg ...interface{}) {
stdlog.Printf("[FATAL] %s %s", fmt.Sprint(msg...), pretty(b.fields))
os.Exit(1)
}
// Panic log message (and exit)
func (b *simple) Panic(msg ...interface{}) {
stdlog.Printf("[PANIC] %s %s", fmt.Sprint(msg...), pretty(b.fields))
panic(fmt.Sprint(msg...))
}
// Debugln log line message
func (b *simple) Debugln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[DEBUG]", a[:len(a)-1], pretty(b.fields))
}
// Infoln log line message
func (b *simple) Infoln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[INFO]", a[:len(a)-1], pretty(b.fields))
}
// Warnln log line message
func (b *simple) Warnln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[WARN]", a[:len(a)-1], pretty(b.fields))
}
// Errorln log line message
func (b *simple) Errorln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[ERROR]", a[:len(a)-1], pretty(b.fields))
}
// Fatalln log line message
func (b *simple) Fatalln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[FATAL]", a[:len(a)-1], pretty(b.fields))
os.Exit(1)
}
// Panicln log line message
func (b *simple) Panicln(msg ...interface{}) {
a := fmt.Sprintln(msg...)
stdlog.Println("[PANIC]", a[:len(a)-1], pretty(b.fields))
panic(a[:len(a)-1])
}
// Debugf log message with formatting
func (b *simple) Debugf(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[DEBUG] "+format, args...), " ", pretty(b.fields))
}
// Infof log message with formatting
func (b *simple) Infof(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[INFO] "+format, args...), " ", pretty(b.fields))
}
// Warnf log message with formatting
func (b *simple) Warnf(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[WARN] "+format, args...), " ", pretty(b.fields))
}
// Errorf log message with formatting
func (b *simple) Errorf(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[ERROR] "+format, args...), " ", pretty(b.fields))
}
// Fatalf log message with formatting
func (b *simple) Fatalf(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[FATAL] "+format, args...), " ", pretty(b.fields))
os.Exit(1)
}
// Panicf log message with formatting
func (b *simple) Panicf(format string, args ...interface{}) {
stdlog.Print(fmt.Sprintf("[PANIC] "+format, args...), " ", pretty(b.fields))
panic(fmt.Sprintf(format, args...))
}
// helper for pretty printing of fields
func pretty(m map[string]interface{}) string {
if len(m) < 1 {
return ""
}
s := ""
for k, v := range m {
s += fmt.Sprintf("%s=%v ", k, v)
}
return s[:len(s)-1]
}
/*************
No-Op Logger
*************/
type noop struct{}
// NewNoop creates a no-op logger that can be used to silence
// all logging from this library. Also useful in tests.
func NewNoop() Logger {
return &noop{}
}
// Debug log message no-op
func (n *noop) Debug(msg ...interface{}) {}
// Info log message no-op
func (n *noop) Info(msg ...interface{}) {}
// Warn log message no-op
func (n *noop) Warn(msg ...interface{}) {}
// Error log message no-op
func (n *noop) Error(msg ...interface{}) {}
// Fatal log message no-op
func (n *noop) Fatal(msg ...interface{}) {}
// Panic log message no-op
func (n *noop) Panic(msg ...interface{}) {}
// Debugln line log message no-op
func (n *noop) Debugln(msg ...interface{}) {}
// Infoln line log message no-op
func (n *noop) Infoln(msg ...interface{}) {}
// Warnln line log message no-op
func (n *noop) Warnln(msg ...interface{}) {}
// Errorln line log message no-op
func (n *noop) Errorln(msg ...interface{}) {}
// Fatalln line log message no-op
func (n *noop) Fatalln(msg ...interface{}) {}
// Panicln line log message no-op
func (n *noop) Panicln(msg ...interface{}) {}
// Debugf log message with formatting no-op
func (n *noop) Debugf(format string, args ...interface{}) {}
// Infof log message with formatting no-op
func (n *noop) Infof(format string, args ...interface{}) {}
// Warnf log message with formatting no-op
func (n *noop) Warnf(format string, args ...interface{}) {}
// Errorf log message with formatting no-op
func (n *noop) Errorf(format string, args ...interface{}) {}
// Fatalf log message with formatting no-op
func (n *noop) Fatalf(format string, args ...interface{}) {}
// Panicf log message with formatting no-op
func (n *noop) Panicf(format string, args ...interface{}) {}
// WithFields no-op
func (n *noop) WithFields(fields Fields) Logger { return n }