-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlog.go
More file actions
329 lines (292 loc) · 7.91 KB
/
log.go
File metadata and controls
329 lines (292 loc) · 7.91 KB
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
package log
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
)
type (
// Log entry
Entry struct {
Time time.Time
Severity Severity
KeyVals kvList
}
// Logger implementation
logger struct {
options *options
lock sync.Mutex
keyvals kvList
entries []*Entry
flushed bool
}
// Log severity enum
Severity int
// private type for context keys
ctxKey int
)
const (
SeverityDebug Severity = iota + 1
SeverityInfo
SeverityWarn
SeverityError
)
// Be kind to tests
var (
timeNow = time.Now
timeSince = time.Since
osExit = os.Exit
)
// Debug writes the key/value pairs to the log output if the log context is
// configured to log debug messages (via WithDebug).
func Debug(ctx context.Context, keyvals ...Fielder) {
log(ctx, SeverityDebug, true, keyvals)
}
// Debugf sets the key MessageKey (default "msg") and calls Debug. Arguments
// are handled in the manner of fmt.Printf.
func Debugf(ctx context.Context, format string, v ...any) {
Debug(ctx, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// Print writes the key/value pairs to the log output ignoring buffering.
func Print(ctx context.Context, keyvals ...Fielder) {
log(ctx, SeverityInfo, false, keyvals)
}
// Printf sets the key MessageKey (default "msg") and calls Print. Arguments
// are handled in the manner of fmt.Printf.
func Printf(ctx context.Context, format string, v ...any) {
Print(ctx, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// Info writes the key/value pairs to the log buffer or output if buffering is
// disabled.
func Info(ctx context.Context, keyvals ...Fielder) {
log(ctx, SeverityInfo, true, keyvals)
}
// Infof sets the key MessageKey (default "msg") and calls Info. Arguments are
// handled in the manner of fmt.Printf.
func Infof(ctx context.Context, format string, v ...any) {
Info(ctx, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// Warn writes the key/value pairs to the log buffer or output if buffering is
// disabled, with SeverityWarn.
func Warn(ctx context.Context, keyvals ...Fielder) {
log(ctx, SeverityWarn, true, keyvals)
}
// Warnf sets the key MessageKey (default "msg") and calls Warn. Arguments are
// handled in the manner of fmt.Printf.
func Warnf(ctx context.Context, format string, v ...any) {
Warn(ctx, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// Error flushes the log buffer and disables buffering if not already disabled.
// Error then sets the ErrorMessageKey (default "err") key with the given error
// and writes the key/value pairs to the log output.
func Error(ctx context.Context, err error, keyvals ...Fielder) {
FlushAndDisableBuffering(ctx)
if err != nil {
kvs := make([]Fielder, len(keyvals)+1)
copy(kvs[1:], keyvals)
kvs[0] = KV{ErrorMessageKey, err.Error()}
keyvals = kvs
}
log(ctx, SeverityError, true, keyvals)
}
// Errorf sets the key MessageKey (default "msg") and calls Error. Arguments
// are handled in the manner of fmt.Printf.
func Errorf(ctx context.Context, err error, format string, v ...any) {
Error(ctx, err, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// Fatal is equivalent to Error followed by a call to os.Exit(1)
func Fatal(ctx context.Context, err error, keyvals ...Fielder) {
Error(ctx, err, keyvals...)
osExit(1)
}
// Fatalf is equivalent to Errorf followed by a call to os.Exit(1)
func Fatalf(ctx context.Context, err error, format string, v ...any) {
Fatal(ctx, err, KV{MessageKey, fmt.Sprintf(format, v...)})
}
// With creates a copy of the given log context and appends the given key/value
// pairs to it. Values must be strings, numbers, booleans, nil or a slice of
// these types.
func With(ctx context.Context, keyvals ...Fielder) context.Context {
v := ctx.Value(ctxLogger)
if v == nil {
return ctx
}
l := v.(*logger)
l.lock.Lock()
defer l.lock.Unlock()
newLogger := logger{
options: l.options,
entries: l.entries,
keyvals: l.keyvals.merge(keyvals),
flushed: l.flushed,
}
if l.options.disableBuffering != nil && l.options.disableBuffering(ctx) {
l.flush()
newLogger.flushed = true
} else {
newLogger.entries = make([]*Entry, len(l.entries))
copy(newLogger.entries, l.entries)
}
return context.WithValue(ctx, ctxLogger, &newLogger)
}
// FlushAndDisableBuffering flushes the log entries to the writer and stops
// buffering the given context.
func FlushAndDisableBuffering(ctx context.Context) {
v := ctx.Value(ctxLogger)
if v == nil {
return // do nothing if context isn't initialized
}
l := v.(*logger)
l.lock.Lock()
defer l.lock.Unlock()
l.flush()
}
func (l *logger) writeEntry(e *Entry) {
l.options.w.Write(l.options.format(e)) // nolint: errcheck
}
func (l *logger) flush() {
if l.flushed {
return
}
for _, e := range l.entries {
l.writeEntry(e)
}
l.entries = nil // free up memory
l.flushed = true
}
func log(ctx context.Context, sev Severity, buffer bool, fielders []Fielder) {
v := ctx.Value(ctxLogger)
if v == nil {
return // do nothing if context isn't initialized
}
l := v.(*logger)
l.lock.Lock()
defer l.lock.Unlock()
if !l.options.debug && sev == SeverityDebug {
return
}
if l.options.debug && !l.flushed {
l.flush()
}
var keyvals kvList
keyvals = keyvals.merge(fielders)
keyvals = append(l.keyvals, keyvals...)
keyvals = append(l.options.keyvals, keyvals...)
for _, fn := range l.options.kvfuncs {
keyvals = append(keyvals, fn(ctx)...)
}
truncate(keyvals, l.options.maxsize)
e := &Entry{timeNow().UTC(), sev, keyvals}
if l.flushed || !buffer {
l.writeEntry(e)
return
}
l.entries = append(l.entries, e)
}
// String returns a string representation of the log severity.
func (l Severity) String() string {
switch l {
case SeverityDebug:
return "debug"
case SeverityInfo:
return "info"
case SeverityWarn:
return "warn"
case SeverityError:
return "error"
default:
return "<INVALID>"
}
}
// Code returns a 4-character code for the log severity.
func (l Severity) Code() string {
switch l {
case SeverityDebug:
return "DEBG"
case SeverityInfo:
return "INFO"
case SeverityWarn:
return "WARN"
case SeverityError:
return "ERRO"
default:
return "<INVALID>"
}
}
// Color returns an escape sequence that colors the output for the given
// severity.
func (l Severity) Color() string {
switch l {
case SeverityDebug:
return "\033[37m"
case SeverityInfo:
return "\033[34m"
case SeverityWarn:
return "\033[33m"
case SeverityError:
return "\033[1;31m"
default:
return ""
}
}
const truncationSuffix = " ... <clue/log.truncated>"
var errTruncated = errors.New("truncated value")
// truncate makes sure that all string values in keyvals are no longer than
// maxsize and that all slice values are truncated to maxsize.
//
// Note: This could get very complicated very quickly (there could be different
// max values for strings and slices, it could compute total size for slices vs.
// size for each element, could recurse further etc.) - the point is to protect
// against obvious mistakes - not to implement a bullet-proof solution.
func truncate(keyvals []KV, maxsize int) {
if len(keyvals) > maxsize {
keyvals = keyvals[:maxsize]
keyvals = append(keyvals, KV{"log", truncationSuffix})
}
for i, kv := range keyvals {
switch kv.V.(type) {
case int, int8, int16, int32, int64:
continue
case uint, uint8, uint16, uint32, uint64:
continue
case float32, float64:
continue
case bool:
continue
case nil:
continue
default:
var buf bytes.Buffer
_, err := fmt.Fprintf(newLimitWriter(&buf, maxsize), "%v", kv.V)
if errors.Is(err, errTruncated) {
fmt.Fprint(&buf, truncationSuffix)
keyvals[i] = KV{K: kv.K, V: buf.String()}
}
}
}
}
type limitWriter struct {
io.Writer
max int
n int
}
func newLimitWriter(w io.Writer, max int) io.Writer {
return &limitWriter{
Writer: w,
max: max,
}
}
func (lw *limitWriter) Write(b []byte) (int, error) {
newLen := lw.n + len(b)
if newLen > lw.max {
b = b[:lw.max-lw.n]
lw.Writer.Write(b) // nolint: errcheck
return lw.max - lw.n, errTruncated
}
lw.n = newLen
return lw.Writer.Write(b)
}