-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
logger.go
407 lines (355 loc) · 10.6 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
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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logutil
import (
"bytes"
"fmt"
"io"
"runtime"
"strings"
"sync"
"time"
"vitess.io/vitess/go/protoutil"
logutilpb "vitess.io/vitess/go/vt/proto/logutil"
)
// Logger defines the interface to use for our logging interface.
// All methods should be thread safe (i.e. multiple go routines can
// call these methods simultaneously).
type Logger interface {
// Infof logs at INFO level. A newline is appended if missing.
Infof(format string, v ...any)
// Warningf logs at WARNING level. A newline is appended if missing.
Warningf(format string, v ...any)
// Errorf logs at ERROR level. A newline is appended if missing.
Errorf(format string, v ...any)
// Errorf2 logs an error with stack traces at ERROR level. A newline is appended if missing.
Errorf2(e error, message string, v ...any)
Error(e error)
// Printf will just display information on stdout when possible.
// No newline is appended.
Printf(format string, v ...any)
// InfoDepth allows call frame depth to be adjusted when logging to INFO.
InfoDepth(depth int, s string)
// WarningDepth allows call frame depth to be adjusted when logging to WARNING.
WarningDepth(depth int, s string)
// ErrorDepth allows call frame depth to be adjusted when logging to ERROR.
ErrorDepth(depth int, s string)
}
// EventToBuffer formats an individual Event into a buffer, without the
// final '\n'
func EventToBuffer(event *logutilpb.Event, buf *bytes.Buffer) {
// Avoid Fprintf, for speed. The format is so simple that we
// can do it quickly by hand. It's worth about 3X. Fprintf is hard.
// Lmmdd hh:mm:ss.uuuuuu file:line]
switch event.Level {
case logutilpb.Level_INFO:
buf.WriteByte('I')
case logutilpb.Level_WARNING:
buf.WriteByte('W')
case logutilpb.Level_ERROR:
buf.WriteByte('E')
case logutilpb.Level_CONSOLE:
buf.WriteString(event.Value)
return
}
t := protoutil.TimeFromProto(event.Time).UTC()
_, month, day := t.Date()
hour, minute, second := t.Clock()
twoDigits(buf, int(month))
twoDigits(buf, day)
buf.WriteByte(' ')
twoDigits(buf, hour)
buf.WriteByte(':')
twoDigits(buf, minute)
buf.WriteByte(':')
twoDigits(buf, second)
buf.WriteByte('.')
nDigits(buf, 6, t.Nanosecond()/1000, '0')
buf.WriteByte(' ')
buf.WriteString(event.File)
buf.WriteByte(':')
someDigits(buf, event.Line)
buf.WriteByte(']')
buf.WriteByte(' ')
buf.WriteString(event.Value)
}
// EventString returns the line in one string
func EventString(event *logutilpb.Event) string {
buf := new(bytes.Buffer)
EventToBuffer(event, buf)
return buf.String()
}
// LogEvent sends an event to a Logger, using the level specified in the event.
// The event struct is converted to a string with EventString().
func LogEvent(logger Logger, event *logutilpb.Event) {
switch event.Level {
case logutilpb.Level_INFO:
logger.InfoDepth(1, EventString(event))
case logutilpb.Level_WARNING:
logger.WarningDepth(1, EventString(event))
case logutilpb.Level_ERROR:
logger.ErrorDepth(1, EventString(event))
case logutilpb.Level_CONSOLE:
// Note we can't just pass the string, because it might contain '%'.
logger.Printf("%s", EventString(event))
}
}
// CallbackLogger is a logger that sends the logging event to a callback
// for consumption.
type CallbackLogger struct {
f func(*logutilpb.Event)
}
// NewCallbackLogger returns a new logger to the given callback.
// Note this and the other objects using this object should either
// all use pointer receivers, or non-pointer receivers.
// (that is ChannelLogger and MemoryLogger). That way they can share the
// 'depth' parameter freely. In this code now, they all use pointer receivers.
func NewCallbackLogger(f func(*logutilpb.Event)) *CallbackLogger {
return &CallbackLogger{f}
}
// InfoDepth is part of the Logger interface.
func (cl *CallbackLogger) InfoDepth(depth int, s string) {
file, line := fileAndLine(2 + depth)
cl.f(&logutilpb.Event{
Time: protoutil.TimeToProto(time.Now()),
Level: logutilpb.Level_INFO,
File: file,
Line: line,
Value: s,
})
}
// WarningDepth is part of the Logger interface
func (cl *CallbackLogger) WarningDepth(depth int, s string) {
file, line := fileAndLine(2 + depth)
cl.f(&logutilpb.Event{
Time: protoutil.TimeToProto(time.Now()),
Level: logutilpb.Level_WARNING,
File: file,
Line: line,
Value: s,
})
}
// ErrorDepth is part of the Logger interface
func (cl *CallbackLogger) ErrorDepth(depth int, s string) {
file, line := fileAndLine(2 + depth)
cl.f(&logutilpb.Event{
Time: protoutil.TimeToProto(time.Now()),
Level: logutilpb.Level_ERROR,
File: file,
Line: line,
Value: s,
})
}
// Infof is part of the Logger interface.
func (cl *CallbackLogger) Infof(format string, v ...any) {
cl.InfoDepth(1, fmt.Sprintf(format, v...))
}
// Warningf is part of the Logger interface.
func (cl *CallbackLogger) Warningf(format string, v ...any) {
cl.WarningDepth(1, fmt.Sprintf(format, v...))
}
// Errorf is part of the Logger interface.
func (cl *CallbackLogger) Errorf(format string, v ...any) {
cl.ErrorDepth(1, fmt.Sprintf(format, v...))
}
// Errorf2 is part of the Logger interface
func (cl *CallbackLogger) Errorf2(err error, format string, v ...any) {
cl.ErrorDepth(1, fmt.Sprintf(format+": %+v", append(v, err)))
}
// Error is part of the Logger interface
func (cl *CallbackLogger) Error(err error) {
cl.ErrorDepth(1, fmt.Sprintf("%+v", err))
}
// Printf is part of the Logger interface.
func (cl *CallbackLogger) Printf(format string, v ...any) {
file, line := fileAndLine(2)
cl.f(&logutilpb.Event{
Time: protoutil.TimeToProto(time.Now()),
Level: logutilpb.Level_CONSOLE,
File: file,
Line: line,
Value: fmt.Sprintf(format, v...),
})
}
// ChannelLogger is a Logger that sends the logging events through a channel for
// consumption.
type ChannelLogger struct {
CallbackLogger
C chan *logutilpb.Event
}
// NewChannelLogger returns a CallbackLogger which will write the data
// on a channel
func NewChannelLogger(size int) *ChannelLogger {
c := make(chan *logutilpb.Event, size)
return &ChannelLogger{
CallbackLogger: CallbackLogger{
f: func(e *logutilpb.Event) {
c <- e
},
},
C: c,
}
}
// MemoryLogger keeps the logging events in memory.
// All protected by a mutex.
type MemoryLogger struct {
CallbackLogger
// mu protects the Events
mu sync.Mutex
Events []*logutilpb.Event
}
// NewMemoryLogger returns a new MemoryLogger
func NewMemoryLogger() *MemoryLogger {
ml := &MemoryLogger{}
ml.CallbackLogger.f = func(e *logutilpb.Event) {
ml.mu.Lock()
defer ml.mu.Unlock()
ml.Events = append(ml.Events, e)
}
return ml
}
// String returns all the lines in one String, separated by '\n'
func (ml *MemoryLogger) String() string {
buf := new(bytes.Buffer)
ml.mu.Lock()
defer ml.mu.Unlock()
for _, event := range ml.Events {
EventToBuffer(event, buf)
buf.WriteByte('\n')
}
return buf.String()
}
// Clear clears the logs.
func (ml *MemoryLogger) Clear() {
ml.mu.Lock()
ml.Events = nil
ml.mu.Unlock()
}
// LoggerWriter is an adapter that implements the io.Writer interface.
type LoggerWriter struct {
logger Logger
}
// NewLoggerWriter returns an io.Writer on top of the logger
func NewLoggerWriter(logger Logger) io.Writer {
return LoggerWriter{
logger: logger,
}
}
// Write implements io.Writer
func (lw LoggerWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
lw.logger.Printf("%v", string(p))
return len(p), nil
}
// TeeLogger is a Logger that sends its logs to two underlying logger
type TeeLogger struct {
One, Two Logger
}
// NewTeeLogger returns a logger that sends its logs to both loggers
func NewTeeLogger(one, two Logger) *TeeLogger {
return &TeeLogger{
One: one,
Two: two,
}
}
// InfoDepth is part of the Logger interface
func (tl *TeeLogger) InfoDepth(depth int, s string) {
tl.One.InfoDepth(1+depth, s)
tl.Two.InfoDepth(1+depth, s)
}
// WarningDepth is part of the Logger interface
func (tl *TeeLogger) WarningDepth(depth int, s string) {
tl.One.WarningDepth(1+depth, s)
tl.Two.WarningDepth(1+depth, s)
}
// ErrorDepth is part of the Logger interface
func (tl *TeeLogger) ErrorDepth(depth int, s string) {
tl.One.ErrorDepth(1+depth, s)
tl.Two.ErrorDepth(1+depth, s)
}
// Infof is part of the Logger interface
func (tl *TeeLogger) Infof(format string, v ...any) {
tl.InfoDepth(1, fmt.Sprintf(format, v...))
}
// Warningf is part of the Logger interface
func (tl *TeeLogger) Warningf(format string, v ...any) {
tl.WarningDepth(1, fmt.Sprintf(format, v...))
}
// Errorf is part of the Logger interface
func (tl *TeeLogger) Errorf(format string, v ...any) {
tl.ErrorDepth(1, fmt.Sprintf(format, v...))
}
// Errorf2 is part of the Logger interface
func (tl *TeeLogger) Errorf2(err error, format string, v ...any) {
tl.ErrorDepth(1, fmt.Sprintf(format+": %+v", append(v, err)))
}
// Error is part of the Logger interface
func (tl *TeeLogger) Error(err error) {
tl.ErrorDepth(1, fmt.Sprintf("%+v", err))
}
// Printf is part of the Logger interface
func (tl *TeeLogger) Printf(format string, v ...any) {
tl.One.Printf(format, v...)
tl.Two.Printf(format, v...)
}
// array for fast int -> string conversion
const digits = "0123456789"
// twoDigits adds a zero-prefixed two-digit integer to buf
func twoDigits(buf *bytes.Buffer, value int) {
buf.WriteByte(digits[value/10])
buf.WriteByte(digits[value%10])
}
// nDigits adds an n-digit integer d to buf
// padding with pad on the left.
// It assumes d >= 0.
func nDigits(buf *bytes.Buffer, n, d int, pad byte) {
tmp := make([]byte, n)
j := n - 1
for ; j >= 0 && d > 0; j-- {
tmp[j] = digits[d%10]
d /= 10
}
for ; j >= 0; j-- {
tmp[j] = pad
}
buf.Write(tmp)
}
// someDigits adds a zero-prefixed variable-width integer to buf
func someDigits(buf *bytes.Buffer, d int64) {
// Print into the top, then copy down.
tmp := make([]byte, 10)
j := 10
for {
j--
tmp[j] = digits[d%10]
d /= 10
if d == 0 {
break
}
}
buf.Write(tmp[j:])
}
// fileAndLine returns the caller's file and line 2 levels above
func fileAndLine(depth int) (string, int64) {
_, file, line, ok := runtime.Caller(depth)
if !ok {
return "???", 1
}
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
return file, int64(line)
}