forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
452 lines (374 loc) · 10.6 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
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"fmt"
rlog "github.com/sirupsen/logrus"
"io"
"os"
"path"
"runtime"
"strings"
"time"
)
// SFSessionIDKey is context key of session id
const SFSessionIDKey contextKey = "LOG_SESSION_ID"
// SFSessionUserKey is context key of user id of a session
const SFSessionUserKey contextKey = "LOG_USER"
// map which stores a string which will be used as a log key to the function which
// will be called to get the log value out of the context
var clientLogContextHooks = map[string]ClientLogContextHook{}
// ClientLogContextHook is a client-defined hook that can be used to insert log
// fields based on the Context.
type ClientLogContextHook func(context.Context) string
// RegisterLogContextHook registers a hook that can be used to extract fields
// from the Context and associated with log messages using the provided key. This
// function is not thread-safe and should only be called on startup.
func RegisterLogContextHook(contextKey string, ctxExtractor ClientLogContextHook) {
clientLogContextHooks[contextKey] = ctxExtractor
}
// LogKeys registers string-typed context keys to be written to the logs when
// logger.WithContext is used
var LogKeys = [...]contextKey{SFSessionIDKey, SFSessionUserKey}
// SFLogger Snowflake logger interface to expose FieldLogger defined in logrus
type SFLogger interface {
rlog.Ext1FieldLogger
SetLogLevel(level string) error
GetLogLevel() string
WithContext(ctx context.Context) *rlog.Entry
SetOutput(output io.Writer)
CloseFileOnLoggerReplace(file *os.File) error
Replace(newLogger *SFLogger)
}
// SFCallerPrettyfier to provide base file name and function name from calling frame used in SFLogger
func SFCallerPrettyfier(frame *runtime.Frame) (string, string) {
return path.Base(frame.Function), fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line)
}
type defaultLogger struct {
inner *rlog.Logger
enabled bool
file *os.File
}
// SetLogLevel set logging level for calling defaultLogger
func (log *defaultLogger) SetLogLevel(level string) error {
newEnabled := strings.ToUpper(level) != "OFF"
log.enabled = newEnabled
if newEnabled {
actualLevel, err := rlog.ParseLevel(level)
if err != nil {
return err
}
log.inner.SetLevel(actualLevel)
}
return nil
}
// GetLogLevel return current log level
func (log *defaultLogger) GetLogLevel() string {
if !log.enabled {
return "OFF"
}
return log.inner.GetLevel().String()
}
// CloseFileOnLoggerReplace set a file to be closed when releasing resources occupied by the logger
func (log *defaultLogger) CloseFileOnLoggerReplace(file *os.File) error {
if log.file != nil && log.file != file {
return fmt.Errorf("could not set a file to close on logger reset because there were already set one")
}
log.file = file
return nil
}
// Replace substitute logger by a given one
func (log *defaultLogger) Replace(newLogger *SFLogger) {
SetLogger(newLogger)
closeLogFile(log.file)
}
func closeLogFile(file *os.File) {
if file != nil {
err := file.Close()
if err != nil {
logger.Errorf("failed to close log file: %s", err)
}
}
}
// WithContext return Entry to include fields in context
func (log *defaultLogger) WithContext(ctx context.Context) *rlog.Entry {
fields := context2Fields(ctx)
return log.inner.WithFields(*fields)
}
// CreateDefaultLogger return a new instance of SFLogger with default config
func CreateDefaultLogger() SFLogger {
var rLogger = rlog.New()
var formatter = rlog.TextFormatter{CallerPrettyfier: SFCallerPrettyfier}
rLogger.SetReportCaller(true)
rLogger.SetFormatter(&formatter)
var ret = defaultLogger{inner: rLogger, enabled: true}
return &ret //(&ret).(*SFLogger)
}
// WithField allocates a new entry and adds a field to it.
// Debug, Print, Info, Warn, Error, Fatal or Panic must be then applied to
// this new returned entry.
// If you want multiple fields, use `WithFields`.
func (log *defaultLogger) WithField(key string, value interface{}) *rlog.Entry {
return log.inner.WithField(key, value)
}
// Adds a struct of fields to the log entry. All it does is call `WithField` for
// each `Field`.
func (log *defaultLogger) WithFields(fields rlog.Fields) *rlog.Entry {
return log.inner.WithFields(fields)
}
// Add an error as single field to the log entry. All it does is call
// `WithError` for the given `error`.
func (log *defaultLogger) WithError(err error) *rlog.Entry {
return log.inner.WithError(err)
}
// Overrides the time of the log entry.
func (log *defaultLogger) WithTime(t time.Time) *rlog.Entry {
return log.inner.WithTime(t)
}
func (log *defaultLogger) Logf(level rlog.Level, format string, args ...interface{}) {
log.inner.Logf(level, format, args...)
}
func (log *defaultLogger) Tracef(format string, args ...interface{}) {
if log.enabled {
log.inner.Tracef(format, args...)
}
}
func (log *defaultLogger) Debugf(format string, args ...interface{}) {
if log.enabled {
log.inner.Debugf(format, args...)
}
}
func (log *defaultLogger) Infof(format string, args ...interface{}) {
if log.enabled {
log.inner.Infof(format, args...)
}
}
func (log *defaultLogger) Printf(format string, args ...interface{}) {
if log.enabled {
log.inner.Printf(format, args...)
}
}
func (log *defaultLogger) Warnf(format string, args ...interface{}) {
if log.enabled {
log.inner.Warnf(format, args...)
}
}
func (log *defaultLogger) Warningf(format string, args ...interface{}) {
if log.enabled {
log.inner.Warningf(format, args...)
}
}
func (log *defaultLogger) Errorf(format string, args ...interface{}) {
if log.enabled {
log.inner.Errorf(format, args...)
}
}
func (log *defaultLogger) Fatalf(format string, args ...interface{}) {
if log.enabled {
log.inner.Fatalf(format, args...)
}
}
func (log *defaultLogger) Panicf(format string, args ...interface{}) {
if log.enabled {
log.inner.Panicf(format, args...)
}
}
func (log *defaultLogger) Log(level rlog.Level, args ...interface{}) {
log.inner.Log(level, args...)
}
func (log *defaultLogger) LogFn(level rlog.Level, fn rlog.LogFunction) {
log.inner.LogFn(level, fn)
}
func (log *defaultLogger) Trace(args ...interface{}) {
if log.enabled {
log.inner.Trace(args...)
}
}
func (log *defaultLogger) Debug(args ...interface{}) {
if log.enabled {
log.inner.Debug(args...)
}
}
func (log *defaultLogger) Info(args ...interface{}) {
if log.enabled {
log.inner.Info(args...)
}
}
func (log *defaultLogger) Print(args ...interface{}) {
if log.enabled {
log.inner.Print(args...)
}
}
func (log *defaultLogger) Warn(args ...interface{}) {
if log.enabled {
log.inner.Warn(args...)
}
}
func (log *defaultLogger) Warning(args ...interface{}) {
if log.enabled {
log.inner.Warning(args...)
}
}
func (log *defaultLogger) Error(args ...interface{}) {
if log.enabled {
log.inner.Error(args...)
}
}
func (log *defaultLogger) Fatal(args ...interface{}) {
if log.enabled {
log.inner.Fatal(args...)
}
}
func (log *defaultLogger) Panic(args ...interface{}) {
if log.enabled {
log.inner.Panic(args...)
}
}
func (log *defaultLogger) TraceFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.TraceFn(fn)
}
}
func (log *defaultLogger) DebugFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.DebugFn(fn)
}
}
func (log *defaultLogger) InfoFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.InfoFn(fn)
}
}
func (log *defaultLogger) PrintFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.PrintFn(fn)
}
}
func (log *defaultLogger) WarnFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.PrintFn(fn)
}
}
func (log *defaultLogger) WarningFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.WarningFn(fn)
}
}
func (log *defaultLogger) ErrorFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.ErrorFn(fn)
}
}
func (log *defaultLogger) FatalFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.FatalFn(fn)
}
}
func (log *defaultLogger) PanicFn(fn rlog.LogFunction) {
if log.enabled {
log.inner.PanicFn(fn)
}
}
func (log *defaultLogger) Logln(level rlog.Level, args ...interface{}) {
log.inner.Logln(level, args...)
}
func (log *defaultLogger) Traceln(args ...interface{}) {
if log.enabled {
log.inner.Traceln(args...)
}
}
func (log *defaultLogger) Debugln(args ...interface{}) {
if log.enabled {
log.inner.Debugln(args...)
}
}
func (log *defaultLogger) Infoln(args ...interface{}) {
if log.enabled {
log.inner.Infoln(args...)
}
}
func (log *defaultLogger) Println(args ...interface{}) {
if log.enabled {
log.inner.Println(args...)
}
}
func (log *defaultLogger) Warnln(args ...interface{}) {
if log.enabled {
log.inner.Warnln(args...)
}
}
func (log *defaultLogger) Warningln(args ...interface{}) {
if log.enabled {
log.inner.Warningln(args...)
}
}
func (log *defaultLogger) Errorln(args ...interface{}) {
if log.enabled {
log.inner.Errorln(args...)
}
}
func (log *defaultLogger) Fatalln(args ...interface{}) {
if log.enabled {
log.inner.Fatalln(args...)
}
}
func (log *defaultLogger) Panicln(args ...interface{}) {
if log.enabled {
log.inner.Panicln(args...)
}
}
func (log *defaultLogger) Exit(code int) {
log.inner.Exit(code)
}
// SetLevel sets the logger level.
func (log *defaultLogger) SetLevel(level rlog.Level) {
log.inner.SetLevel(level)
}
// GetLevel returns the logger level.
func (log *defaultLogger) GetLevel() rlog.Level {
return log.inner.GetLevel()
}
// AddHook adds a hook to the logger hooks.
func (log *defaultLogger) AddHook(hook rlog.Hook) {
log.inner.AddHook(hook)
}
// IsLevelEnabled checks if the log level of the logger is greater than the level param
func (log *defaultLogger) IsLevelEnabled(level rlog.Level) bool {
return log.inner.IsLevelEnabled(level)
}
// SetFormatter sets the logger formatter.
func (log *defaultLogger) SetFormatter(formatter rlog.Formatter) {
log.inner.SetFormatter(formatter)
}
// SetOutput sets the logger output.
func (log *defaultLogger) SetOutput(output io.Writer) {
log.inner.SetOutput(output)
}
func (log *defaultLogger) SetReportCaller(reportCaller bool) {
log.inner.SetReportCaller(reportCaller)
}
// SetLogger set a new logger of SFLogger interface for gosnowflake
func SetLogger(inLogger *SFLogger) {
logger = *inLogger //.(*defaultLogger)
}
// GetLogger return logger that is not public
func GetLogger() SFLogger {
return logger
}
func context2Fields(ctx context.Context) *rlog.Fields {
var fields = rlog.Fields{}
if ctx == nil {
return &fields
}
for i := 0; i < len(LogKeys); i++ {
if ctx.Value(LogKeys[i]) != nil {
fields[string(LogKeys[i])] = ctx.Value(LogKeys[i])
}
}
for key, hook := range clientLogContextHooks {
if value := hook(ctx); value != "" {
fields[key] = value
}
}
return &fields
}