-
Notifications
You must be signed in to change notification settings - Fork 11
/
log.go
439 lines (375 loc) · 9.39 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
package logf
import (
"fmt"
"io"
stdlog "log"
"os"
"runtime"
"strings"
"sync"
"time"
"unicode/utf8"
)
const (
tsKey = "timestamp="
defaultTSFormat = "2006-01-02T15:04:05.999Z07:00"
// ANSI escape codes for coloring text in console.
reset = "\033[0m"
purple = "\033[35m"
red = "\033[31m"
yellow = "\033[33m"
cyan = "\033[36m"
)
const (
DebugLevel Level = iota + 1 // 1
InfoLevel // 2
WarnLevel // 3
ErrorLevel // 4
FatalLevel // 5
)
// syncWriter is a wrapper around io.Writer that
// synchronizes writes using a mutex.
type syncWriter struct {
sync.Mutex
w io.Writer
}
// Severity level of the log.
type Level int
// Opts represents the config options for the package.
type Opts struct {
Writer io.Writer
Level Level
TimestampFormat string
EnableColor bool
EnableCaller bool
CallerSkipFrameCount int
// These fields will be printed with every log.
DefaultFields []interface{}
}
// Logger is the interface for all log operations related to emitting logs.
type Logger struct {
// Output destination.
out io.Writer
Opts
}
var (
hex = "0123456789abcdef"
bufPool byteBufferPool
exit = func() { os.Exit(1) }
// Map colors with log level.
colorLvlMap = [...]string{
DebugLevel: purple,
InfoLevel: cyan,
WarnLevel: yellow,
ErrorLevel: red,
FatalLevel: red,
}
)
// New instantiates a logger object.
func New(opts Opts) Logger {
// Initialize fallbacks if unspecified by user.
if opts.Writer == nil {
opts.Writer = os.Stderr
}
if opts.TimestampFormat == "" {
opts.TimestampFormat = defaultTSFormat
}
if opts.Level == 0 {
opts.Level = InfoLevel
}
if opts.CallerSkipFrameCount == 0 {
opts.CallerSkipFrameCount = 3
}
if len(opts.DefaultFields)%2 != 0 {
opts.DefaultFields = opts.DefaultFields[0 : len(opts.DefaultFields)-1]
}
return Logger{
out: newSyncWriter(opts.Writer),
Opts: opts,
}
}
// newSyncWriter wraps an io.Writer with syncWriter. It can
// be used as an io.Writer as syncWriter satisfies the io.Writer interface.
func newSyncWriter(in io.Writer) *syncWriter {
if in == nil {
return &syncWriter{w: os.Stderr}
}
return &syncWriter{w: in}
}
// Write synchronously to the underlying io.Writer.
func (w *syncWriter) Write(p []byte) (int, error) {
w.Lock()
n, err := w.w.Write(p)
w.Unlock()
return n, err
}
// String representation of the log severity.
func (l Level) String() string {
switch l {
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warn"
case ErrorLevel:
return "error"
case FatalLevel:
return "fatal"
default:
return "invalid lvl"
}
}
func LevelFromString(lvl string) (Level, error) {
switch lvl {
case "debug":
return DebugLevel, nil
case "info":
return InfoLevel, nil
case "warn":
return WarnLevel, nil
case "error":
return ErrorLevel, nil
case "fatal":
return FatalLevel, nil
default:
return 0, fmt.Errorf("invalid level")
}
}
// Debug emits a debug log line.
func (l Logger) Debug(msg string, fields ...interface{}) {
l.handleLog(msg, DebugLevel, fields...)
}
// Info emits a info log line.
func (l Logger) Info(msg string, fields ...interface{}) {
l.handleLog(msg, InfoLevel, fields...)
}
// Warn emits a warning log line.
func (l Logger) Warn(msg string, fields ...interface{}) {
l.handleLog(msg, WarnLevel, fields...)
}
// Error emits an error log line.
func (l Logger) Error(msg string, fields ...interface{}) {
l.handleLog(msg, ErrorLevel, fields...)
}
// Fatal emits a fatal level log line.
// It aborts the current program with an exit code of 1.
func (l Logger) Fatal(msg string, fields ...interface{}) {
l.handleLog(msg, FatalLevel, fields...)
exit()
}
// handleLog emits the log after filtering log level
// and applying formatting of the fields.
func (l Logger) handleLog(msg string, lvl Level, fields ...interface{}) {
// Discard the log if the verbosity is higher.
// For eg, if the lvl is `3` (error), but the incoming message is `0` (debug), skip it.
if lvl < l.Opts.Level {
return
}
// Get a buffer from the pool.
buf := bufPool.Get()
// Write fixed keys to the buffer before writing user provided ones.
writeTimeToBuf(buf, l.Opts.TimestampFormat, lvl, l.Opts.EnableColor)
writeToBuf(buf, "level", lvl, lvl, l.Opts.EnableColor, true)
writeStringToBuf(buf, "message", msg, lvl, l.Opts.EnableColor, true)
if l.Opts.EnableCaller {
writeCallerToBuf(buf, "caller", l.Opts.CallerSkipFrameCount, lvl, l.EnableColor, true)
}
// Format the line as logfmt.
var (
count int // to find out if this is the last key in while itering fields.
fieldCount = len(l.DefaultFields) + len(fields)
key string
)
// If there are odd number of fields, ignore the last.
if fieldCount%2 != 0 {
fields = fields[0 : len(fields)-1]
}
for i := range l.DefaultFields {
space := false
if count != fieldCount-1 {
space = true
}
if i%2 == 0 {
key = l.DefaultFields[i].(string)
continue
}
writeToBuf(buf, key, l.DefaultFields[i], lvl, l.Opts.EnableColor, space)
count++
}
for i := range fields {
space := false
if count != fieldCount-1 {
space = true
}
if i%2 == 0 {
key = fields[i].(string)
continue
}
writeToBuf(buf, key, fields[i], lvl, l.Opts.EnableColor, space)
count++
}
buf.AppendString("\n")
_, err := l.out.Write(buf.Bytes())
if err != nil {
// Should ideally never happen.
stdlog.Printf("error logging: %v", err)
}
// Put the writer back in the pool. It resets the underlying byte buffer.
bufPool.Put(buf)
}
// writeTimeToBuf writes timestamp key + timestamp into buffer.
func writeTimeToBuf(buf *byteBuffer, format string, lvl Level, color bool) {
if color {
buf.AppendString(getColoredKey(tsKey, lvl))
} else {
buf.AppendString(tsKey)
}
buf.AppendTime(time.Now(), format)
buf.AppendByte(' ')
}
// writeStringToBuf takes key, value and additional options to write to the buffer in logfmt.
func writeStringToBuf(buf *byteBuffer, key, val string, lvl Level, color, space bool) {
if color {
escapeAndWriteString(buf, getColoredKey(key, lvl))
} else {
escapeAndWriteString(buf, key)
}
buf.AppendByte('=')
escapeAndWriteString(buf, val)
if space {
buf.AppendByte(' ')
}
}
func writeCallerToBuf(buf *byteBuffer, key string, depth int, lvl Level, color, space bool) {
_, file, line, ok := runtime.Caller(depth)
if !ok {
file = "???"
line = 0
}
if color {
buf.AppendString(getColoredKey(key, lvl))
} else {
buf.AppendString(key)
}
buf.AppendByte('=')
escapeAndWriteString(buf, file)
buf.AppendByte(':')
buf.AppendInt(int64(line))
if space {
buf.AppendByte(' ')
}
}
// writeToBuf takes key, value and additional options to write to the buffer in logfmt.
func writeToBuf(buf *byteBuffer, key string, val interface{}, lvl Level, color, space bool) {
if color {
escapeAndWriteString(buf, getColoredKey(key, lvl))
} else {
escapeAndWriteString(buf, key)
}
buf.AppendByte('=')
switch v := val.(type) {
case nil:
buf.AppendString("null")
case []byte:
escapeAndWriteString(buf, string(v))
case string:
escapeAndWriteString(buf, v)
case int:
buf.AppendInt(int64(v))
case int8:
buf.AppendInt(int64(v))
case int16:
buf.AppendInt(int64(v))
case int32:
buf.AppendInt(int64(v))
case int64:
buf.AppendInt(v)
case float32:
buf.AppendFloat(float64(v), 32)
case float64:
buf.AppendFloat(v, 64)
case bool:
buf.AppendBool(v)
case error:
escapeAndWriteString(buf, v.Error())
case fmt.Stringer:
escapeAndWriteString(buf, v.String())
default:
escapeAndWriteString(buf, fmt.Sprintf("%v", val))
}
if space {
buf.AppendByte(' ')
}
}
// escapeAndWriteString escapes the string if interface{} unwanted chars are there.
func escapeAndWriteString(buf *byteBuffer, s string) {
idx := strings.IndexFunc(s, checkEscapingRune)
if idx != -1 || s == "null" {
writeQuotedString(buf, s)
return
}
buf.AppendString(s)
}
// getColoredKey returns a color formatter key based on the log level.
func getColoredKey(k string, lvl Level) string {
return colorLvlMap[lvl] + k + reset
}
// checkEscapingRune returns true if the rune is to be escaped.
func checkEscapingRune(r rune) bool {
return r == '=' || r == ' ' || r == '"' || r == utf8.RuneError
}
// writeQuotedString quotes a string before writing to the buffer.
// Taken from: https://github.com/go-logfmt/logfmt/blob/99455b83edb21b32a1f1c0a32f5001b77487b721/jsonstring.go#L95
func writeQuotedString(buf *byteBuffer, s string) {
buf.AppendByte('"')
start := 0
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
if 0x20 <= b && b != '\\' && b != '"' {
i++
continue
}
if start < i {
buf.AppendString(s[start:i])
}
switch b {
case '\\', '"':
buf.AppendByte('\\')
buf.AppendByte(b)
case '\n':
buf.AppendByte('\\')
buf.AppendByte('n')
case '\r':
buf.AppendByte('\\')
buf.AppendByte('r')
case '\t':
buf.AppendByte('\\')
buf.AppendByte('t')
default:
// This encodes bytes < 0x20 except for \n, \r, and \t.
buf.AppendString(`\u00`)
buf.AppendByte(hex[b>>4])
buf.AppendByte(hex[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError {
if start < i {
buf.AppendString(s[start:i])
}
buf.AppendString(`\ufffd`)
i += size
start = i
continue
}
i += size
}
if start < len(s) {
buf.AppendString(s[start:])
}
buf.AppendByte('"')
}