This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
logger.go
247 lines (199 loc) · 6.29 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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package logger
import (
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
"time"
"github.com/featurebasedb/featurebase/v3/monitor"
)
const RFC3339UsecTz0 = "2006-01-02T15:04:05.000000Z07:00"
// Ensure nopLogger implements interface.
var _ Logger = &nopLogger{}
// Logger represents an interface for a shared logger.
type Logger interface {
Printf(format string, v ...interface{}) // backward compatibility
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Panicf(format string, v ...interface{})
// WithPrefix returns a new Logger with the same configuration as
// this one, but all logs will have the given prefix.
WithPrefix(prefix string) Logger
}
const (
LevelPanic = iota
LevelError
LevelWarn
LevelInfo
LevelDebug
)
func LevelPrefix(level int) string {
return [...]string{"PANIC: ", "ERROR: ", "WARN: ", "INFO: ", "DEBUG: "}[level]
}
var StderrLogger = NewStandardLogger(os.Stderr)
// NopLogger represents a Logger that doesn't do anything.
var NopLogger Logger = &nopLogger{}
type nopLogger struct{}
// Printf is a no-op implementation of the Logger Printf method.
func (n *nopLogger) Printf(format string, v ...interface{}) {}
// Debugf is a no-op implementation of the Logger Debugf method.
func (n *nopLogger) Debugf(format string, v ...interface{}) {}
// Infof is a no-op implementation of the Logger Infof method.
func (n *nopLogger) Infof(format string, v ...interface{}) {}
// Warnf is a no-op implementation of the Logger Warnf method.
func (n *nopLogger) Warnf(format string, v ...interface{}) {}
// Errorf is a no-op implementation of the Logger Errorf method.
func (n *nopLogger) Errorf(format string, v ...interface{}) {}
// Panicf is a no-op implementation of the Logger Panicf method.
func (n *nopLogger) Panicf(format string, v ...interface{}) {}
func (n *nopLogger) WithPrefix(prefix string) Logger {
return n
}
// standardLogger is a basic implementation of Logger based on log.Logger.
type standardLogger struct {
logger *log.Logger
verbosity int
prefix string
w io.Writer
}
// write in UTC with constant width and microsecond resolution.
type formatLog struct {
w io.Writer
}
func (fl formatLog) Write(bytes []byte) (int, error) {
return fmt.Fprintf(fl.w, "%v %v", time.Now().UTC().Format(RFC3339UsecTz0), string(bytes))
}
func newStandardLogger(w io.Writer, verbosity int, prefix string) *standardLogger {
logger := log.New(w, prefix, 0)
logger.SetOutput(formatLog{w: w})
return &standardLogger{
logger: logger,
verbosity: verbosity,
prefix: prefix,
w: w,
}
}
func NewStandardLogger(w io.Writer) *standardLogger {
return newStandardLogger(w, LevelInfo, "")
}
func NewVerboseLogger(w io.Writer) *standardLogger {
return newStandardLogger(w, LevelDebug, "")
}
func (s *standardLogger) printf(level int, format string, v ...interface{}) {
if level > s.verbosity {
return
}
if monitor.IsOn() {
// intercepts the log message and sends it to the monitor
monitor.CaptureException(level, format, v...)
}
s.logger.Printf(LevelPrefix(level)+format, v...)
}
func (s *standardLogger) Printf(format string, v ...interface{}) {
s.printf(LevelInfo, format, v...)
}
func (s *standardLogger) Debugf(format string, v ...interface{}) {
s.printf(LevelDebug, format, v...)
}
func (s *standardLogger) Infof(format string, v ...interface{}) {
s.printf(LevelInfo, format, v...)
}
func (s *standardLogger) Warnf(format string, v ...interface{}) {
s.printf(LevelWarn, format, v...)
}
func (s *standardLogger) Errorf(format string, v ...interface{}) {
s.printf(LevelError, format, v...)
}
func (s *standardLogger) Panicf(format string, v ...interface{}) {
s.printf(LevelPanic, format, v...)
}
func (s *standardLogger) Logger() *log.Logger {
return s.logger
}
func (s *standardLogger) WithPrefix(prefix string) Logger {
return newStandardLogger(s.w, s.verbosity, prefix)
}
// Logfer is a thing that has only a Logf() method, like for instance,
// testing.T or testing.B.
type Logfer interface {
Logf(format string, v ...interface{})
}
// LogfLogger is a test logger that wraps something that has a Logf interface
// and makes it act like our logger.
type LogfLogger struct {
wrapped Logfer
}
func (ll *LogfLogger) Printf(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
func (ll *LogfLogger) Debugf(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
func (ll *LogfLogger) Infof(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
func (ll *LogfLogger) Warnf(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
func (ll *LogfLogger) Errorf(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
func (ll *LogfLogger) Panicf(format string, v ...interface{}) {
ll.wrapped.Logf(format, v...)
}
// WithPrefix does nothing for LogfLogger because I'm lazy.
func (ll *LogfLogger) WithPrefix(prefix string) Logger {
return ll
}
func NewLogfLogger(l Logfer) *LogfLogger {
return &LogfLogger{wrapped: l}
}
// bufferLogger represents a test Logger that holds log messages
// in a buffer for review.
type bufferLogger struct {
buf *bytes.Buffer
mu sync.Mutex
}
// NewBufferLogger returns a new instance of bufferLogger.
func NewBufferLogger() *bufferLogger {
return &bufferLogger{
buf: &bytes.Buffer{},
}
}
func (b *bufferLogger) Printf(format string, v ...interface{}) {
b.mu.Lock()
defer b.mu.Unlock()
s := fmt.Sprintf(format, v...)
_, err := b.buf.WriteString(s)
if err != nil {
panic(err)
}
}
func (b *bufferLogger) Debugf(format string, v ...interface{}) {}
func (b *bufferLogger) Infof(format string, v ...interface{}) {
b.Printf(LevelPrefix(1)+format, v...)
}
func (b *bufferLogger) Warnf(format string, v ...interface{}) {
b.Printf(LevelPrefix(2)+format, v...)
}
func (b *bufferLogger) Errorf(format string, v ...interface{}) {
b.Printf(LevelPrefix(3)+format, v...)
}
func (b *bufferLogger) Panicf(format string, v ...interface{}) {
b.Printf(LevelPrefix(4)+format, v...)
}
// WithPrefix does nothing for bufferLogger because I'm lazy.
func (b *bufferLogger) WithPrefix(prefix string) Logger {
return b
}
func (b *bufferLogger) ReadAll() ([]byte, error) {
b.mu.Lock()
defer b.mu.Unlock()
return io.ReadAll(b.buf)
}