-
Notifications
You must be signed in to change notification settings - Fork 9
/
default.go
129 lines (117 loc) · 3.91 KB
/
default.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
package slog
import (
"context"
"sync"
)
var (
defaultLogger Logger = StdlibLogger{}
defaultLoggerM sync.RWMutex
)
func DefaultLogger() Logger {
defaultLoggerM.RLock()
defer defaultLoggerM.RUnlock()
return defaultLogger
}
func SetDefaultLogger(l Logger) {
defaultLoggerM.Lock()
defer defaultLoggerM.Unlock()
defaultLogger = l
}
// Log sends the given Events via the default Logger
func Log(evs ...Event) {
if l := DefaultLogger(); l != nil {
l.Log(evs...)
}
}
// Critical constructs a logging event with critical severity. If the
// default Logger implements the LeveledLogger interface, we forward the
// requests via the Critical interface function. If not, the event is sent
// via the default Logger
func Critical(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Critical(ctx, msg, params...)
} else {
l.Log(Eventf(CriticalSeverity, ctx, msg, params...))
}
}
}
// Error constructs a logging event with error severity. If the
// default Logger implements the LeveledLogger interface, we forward the
// requests via the Error interface function. If not, the event is sent
// via the default Logger
func Error(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Error(ctx, msg, params...)
} else {
l.Log(Eventf(ErrorSeverity, ctx, msg, params...))
}
}
}
// Warn constructs a logging event with warn severity. If the
// DefaultLogger implements the LeveledLogger interface, we forward the
// requests via the Warn interface function. If not, the event is sent
// via the default Logger
func Warn(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Warn(ctx, msg, params...)
} else {
l.Log(Eventf(WarnSeverity, ctx, msg, params...))
}
}
}
// Info constructs a logging event with info severity. If the
// default Logger implements the LeveledLogger interface, we forward the
// requests via the Info interface function. If not, the event is sent
// via the default Logger
func Info(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Info(ctx, msg, params...)
} else {
l.Log(Eventf(InfoSeverity, ctx, msg, params...))
}
}
}
// Debug constructs a logging event with debug severity. If the
// default Logger implements the LeveledLogger interface, we forward the
// requests via the Debug interface function. If not, the event is sent
// via the default Logger
func Debug(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Debug(ctx, msg, params...)
} else {
l.Log(Eventf(DebugSeverity, ctx, msg, params...))
}
}
}
// Trace constructs a logging event with trace severity. If the
// default Logger implements the LeveledLogger interface, we forward the
// requests via the Trace interface function. If not, the event is sent
// via the default Logger
func Trace(ctx context.Context, msg string, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(LeveledLogger); ok {
ll.Trace(ctx, msg, params...)
} else {
l.Log(Eventf(TraceSeverity, ctx, msg, params...))
}
}
}
// FromError constructs a logging event with error severity by default.
// If the default Logger implements the FromErrorLogger interface, we
// forward the requests via the FromError interface function. In this
// case the severity will be inferred from the error.
func FromError(ctx context.Context, msg string, err error, params ...interface{}) {
if l := DefaultLogger(); l != nil {
if ll, ok := l.(FromErrorLogger); ok {
ll.FromError(ctx, msg, err, params...)
} else {
params = append([]interface{}{err}, params...)
l.Log(Eventf(ErrorSeverity, ctx, msg, params...))
}
}
}