-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.go
289 lines (239 loc) · 7.68 KB
/
logging.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
package appwrap
import (
"fmt"
"io"
"net/http"
)
type LogLevel int
const (
LogLevelDebug = LogLevel(iota)
LogLevelInfo
LogLevelWarning
LogLevelError
LogLevelCritical
LogLevelSilence
)
const (
ChildLogName = "pendo.io/child_log"
)
// Interface for system logging using printf() style strings
type Logging interface {
Debugf(format string, args ...interface{}) // Debug message
Infof(format string, args ...interface{}) // Information message
Warningf(format string, args ...interface{}) // Warning message
Errorf(format string, args ...interface{}) // Error message
Criticalf(format string, args ...interface{}) // Critical message
Request(request, url, format string, args ...interface{}) // This is conditionally implemented
AddLabels(labels map[string]string) error // Adds labels to your log message
TraceID() string // Trace ID for current request, or "" if N/A
}
// DataLogging for system logging that can accept json, strings, or structs
type DataLogging interface {
Logging
Debug(data interface{})
Info(data interface{})
Warning(data interface{})
Error(data interface{})
Critical(data interface{})
Close(http.ResponseWriter)
}
// Sometimes, you just need to satify the interface and do nothing.
type NullLogger struct{}
func (nl NullLogger) Debugf(format string, args ...interface{}) {}
func (nl NullLogger) Infof(format string, args ...interface{}) {}
func (nl NullLogger) Warningf(format string, args ...interface{}) {}
func (nl NullLogger) Errorf(format string, args ...interface{}) {}
func (nl NullLogger) Criticalf(format string, args ...interface{}) {}
func (nl NullLogger) Request(request, url, format string, args ...interface{}) {}
func (nl NullLogger) AddLabels(labels map[string]string) error { return nil }
func (nl NullLogger) TraceID() string { return "" }
type FormatLogger struct {
Logf func(format string, args ...interface{})
}
func (fl FormatLogger) Debugf(format string, args ...interface{}) {
fl.Logf("debug: "+format, args...)
}
func (fl FormatLogger) Infof(format string, args ...interface{}) {
fl.Logf("info: "+format, args...)
}
func (fl FormatLogger) Warningf(format string, args ...interface{}) {
fl.Logf("warning: "+format, args...)
}
func (fl FormatLogger) Errorf(format string, args ...interface{}) {
fl.Logf("error: "+format, args...)
}
func (fl FormatLogger) Criticalf(format string, args ...interface{}) {
fl.Logf("critical: "+format, args...)
}
func (fl FormatLogger) Request(request, url, format string, args ...interface{}) {
if len(format) > 0 {
format = " " + format
}
fl.Logf("REQUEST: %s %s"+format, append([]interface{}{request, url}, args...)...)
}
func (fl FormatLogger) AddLabels(labels map[string]string) error {
return nil
}
func (fl FormatLogger) TraceID() string { return "" }
type WriterLogger struct {
FormatLogger
}
func NewWriterLogger(writer io.Writer) Logging {
return WriterLogger{
FormatLogger{
func(format string, args ...interface{}) {
buf := []byte(fmt.Sprintf(format+"\n", args...))
written := 0
for written < len(buf) {
if wrote, err := writer.Write(buf[written:len(buf)]); err != nil {
return
} else {
written += wrote
}
}
},
},
}
}
// LevelLogger is a wrapper for another Logging that will filter based on log level. It calls the inner Logging's
// Request once any log message passes the filter.
type LevelLogger struct {
minlevel LogLevel
wrappedLogger Logging
requestMethod, requestUrl, requestFormat string
requestArgs []interface{}
}
func NewLevelLogger(minlevel LogLevel, wrappedLogger Logging) *LevelLogger {
return &LevelLogger{minlevel: minlevel, wrappedLogger: wrappedLogger}
}
func (ll *LevelLogger) Debugf(format string, args ...interface{}) {
if ll.minlevel <= LogLevelDebug {
ll.emitRequest()
ll.wrappedLogger.Debugf(format, args...)
}
}
func (ll *LevelLogger) Infof(format string, args ...interface{}) {
if ll.minlevel <= LogLevelInfo {
ll.emitRequest()
ll.wrappedLogger.Infof(format, args...)
}
}
func (ll *LevelLogger) Warningf(format string, args ...interface{}) {
if ll.minlevel <= LogLevelWarning {
ll.emitRequest()
ll.wrappedLogger.Warningf(format, args...)
}
}
func (ll *LevelLogger) Errorf(format string, args ...interface{}) {
if ll.minlevel <= LogLevelError {
ll.emitRequest()
ll.wrappedLogger.Errorf(format, args...)
}
}
func (ll *LevelLogger) Criticalf(format string, args ...interface{}) {
if ll.minlevel <= LogLevelCritical {
ll.emitRequest()
ll.wrappedLogger.Criticalf(format, args...)
}
}
func (ll *LevelLogger) Request(request, url, format string, args ...interface{}) {
ll.requestMethod = request
ll.requestUrl = url
ll.requestFormat = format
ll.requestArgs = args
}
func (ll *LevelLogger) AddLabels(labels map[string]string) error {
return ll.wrappedLogger.AddLabels(labels)
}
func (ll *LevelLogger) emitRequest() {
if ll.requestMethod != "" {
ll.wrappedLogger.Request(ll.requestMethod, ll.requestUrl, ll.requestFormat, ll.requestArgs...)
ll.requestMethod = ""
ll.requestUrl = ""
ll.requestFormat = ""
ll.requestArgs = nil
}
}
func (ll *LevelLogger) TraceID() string {
return ll.wrappedLogger.TraceID()
}
func (ll *LevelLogger) SetMinLevel(level LogLevel) {
ll.minlevel = level // unsynchronized write, since we don't care if it takes a bit to be observed by other callers
}
type TeeLogging struct {
Logs []Logging
}
func (tee TeeLogging) Debugf(format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Debugf(format, args...)
}
}
func (tee TeeLogging) Infof(format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Infof(format, args...)
}
}
func (tee TeeLogging) Warningf(format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Warningf(format, args...)
}
}
func (tee TeeLogging) Errorf(format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Errorf(format, args...)
}
}
func (tee TeeLogging) Criticalf(format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Criticalf(format, args...)
}
}
func (tee TeeLogging) Request(request, url, format string, args ...interface{}) {
for _, l := range tee.Logs {
l.Request(request, url, format, args...)
}
}
func (tee TeeLogging) AddLabels(labels map[string]string) error {
for _, l := range tee.Logs {
if err := l.AddLabels(labels); err != nil {
return err
}
}
return nil
}
func (tee TeeLogging) TraceID() string {
for _, log := range tee.Logs {
if id := log.TraceID(); id != "" {
return id
}
}
return ""
}
type PrefixLogger struct {
Logging
Prefix string
}
func (pl PrefixLogger) Debugf(format string, args ...interface{}) {
pl.Logging.Debugf(pl.Prefix+format, args...)
}
func (pl PrefixLogger) Infof(format string, args ...interface{}) {
pl.Logging.Infof(pl.Prefix+format, args...)
}
func (pl PrefixLogger) Warningf(format string, args ...interface{}) {
pl.Logging.Warningf(pl.Prefix+format, args...)
}
func (pl PrefixLogger) Errorf(format string, args ...interface{}) {
pl.Logging.Errorf(pl.Prefix+format, args...)
}
func (pl PrefixLogger) Criticalf(format string, args ...interface{}) {
pl.Logging.Criticalf(pl.Prefix+format, args...)
}
func (pl PrefixLogger) Request(request, url, format string, args ...interface{}) {
pl.Logging.Request(request, url, pl.Prefix+format, args...)
}
func (pl PrefixLogger) AddLabels(labels map[string]string) error {
return pl.Logging.AddLabels(labels)
}
func (pl PrefixLogger) TraceID() string {
return pl.Logging.TraceID()
}