forked from databricks/databricks-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
70 lines (54 loc) · 1.69 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
package logger
import (
"context"
)
var DefaultLogger Logger = &SimpleLogger{}
// Level maps to the logging levels in [Logger].
type Level int
const (
LevelTrace = -8
LevelDebug = -4
LevelInfo = 0
LevelWarn = 4
LevelError = 8
)
type Logger interface {
// Enabled returns if logging at the specified level is enabled.
// This can be used to avoid computating an expensive value if it
// won't be logged anyway.
Enabled(ctx context.Context, level Level) bool
// Tracef logs a formatted string at [LevelTrace].
Tracef(ctx context.Context, format string, v ...any)
// Debugf logs a formatted string at [LevelDebug].
Debugf(ctx context.Context, format string, v ...any)
// Infof logs a formatted string at [LevelInfo].
Infof(ctx context.Context, format string, v ...any)
// Warnf logs a formatted string at [LevelWarn].
Warnf(ctx context.Context, format string, v ...any)
// Errorf logs a formatted string at [LevelError].
Errorf(ctx context.Context, format string, v ...any)
}
// Get returns either the logger configured on the context,
// or the global logger if one isn't defined.
func Get(ctx context.Context) Logger {
logger, ok := FromContext(ctx)
if !ok {
logger = DefaultLogger
}
return logger
}
func Tracef(ctx context.Context, format string, v ...any) {
Get(ctx).Tracef(ctx, format, v...)
}
func Debugf(ctx context.Context, format string, v ...any) {
Get(ctx).Debugf(ctx, format, v...)
}
func Infof(ctx context.Context, format string, v ...any) {
Get(ctx).Infof(ctx, format, v...)
}
func Warnf(ctx context.Context, format string, v ...any) {
Get(ctx).Warnf(ctx, format, v...)
}
func Errorf(ctx context.Context, format string, v ...any) {
Get(ctx).Errorf(ctx, format, v...)
}