-
Notifications
You must be signed in to change notification settings - Fork 0
/
slog.go
130 lines (105 loc) · 3.37 KB
/
slog.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
package slog
var defaultLogger *Logger
var (
logFuncs = map[Level]func(args ...interface{}){
DebugLevel: _debug,
InfoLevel: _info,
WarnLevel: _warn,
ErrorLevel: _error,
CritLevel: _crit,
}
logfFuncs = map[Level]func(msg string, args ...interface{}){
DebugLevel: _debugf,
InfoLevel: _infof,
WarnLevel: _warnf,
ErrorLevel: _errorf,
CritLevel: _critf,
}
logPtrs = map[Level]*func(args ...interface{}){
DebugLevel: &Debug,
InfoLevel: &Info,
WarnLevel: &Warn,
ErrorLevel: &Error,
CritLevel: &Crit,
}
logfPtrs = map[Level]*func(msg string, args ...interface{}){
DebugLevel: &Debugf,
InfoLevel: &Infof,
WarnLevel: &Warnf,
ErrorLevel: &Errorf,
CritLevel: &Critf,
}
)
// SetDefaultLogger set the logger as the defaultLogger.
// The logging functions in this package use it as their logger.
// This function should be called before using the others.
func SetDefaultLogger(l *Logger) {
defaultLogger = l
minLevel := l.GetMinLevel()
for level, f := range logFuncs {
if minLevel <= level {
*logPtrs[level] = f
} else {
*logPtrs[level] = nop
}
}
for level, f := range logfFuncs {
if minLevel <= level {
*logfPtrs[level] = f
} else {
*logfPtrs[level] = nopf
}
}
}
func nop(args ...interface{}) {}
func nopf(msg string, args ...interface{}) {}
// Debug logs a _debug level message. It uses fmt.Fprint() to format args.
var Debug func(args ...interface{})
// Debugf logs a _debug level message. It uses fmt.Fprintf() to format msg and args.
var Debugf func(msg string, args ...interface{})
// Info logs a _info level message. It uses fmt.Fprint() to format args.
var Info func(args ...interface{})
// Infof logs a _info level message. It uses fmt.Fprintf() to format msg and args.
var Infof func(msg string, args ...interface{})
// Warn logs a _warning level message. It uses fmt.Fprint() to format args.
var Warn func(args ...interface{})
// Warnf logs a _warning level message. It uses fmt.Fprintf() to format msg and args.
var Warnf func(msg string, args ...interface{})
// Error logs an _error level message. It uses fmt.Fprint() to format args.
var Error func(args ...interface{})
// Errorf logs a _error level message. It uses fmt.Fprintf() to format msg and args.
var Errorf func(msg string, args ...interface{})
// Crit logs a _critical level message. It uses fmt.Fprint() to format args.
var Crit func(args ...interface{})
// Critf logs a _critical level message. It uses fmt.Fprintf() to format msg and args.
var Critf func(msg string, args ...interface{})
func _debug(args ...interface{}) {
defaultLogger.Log(DebugLevel, "", args...)
}
func _debugf(msg string, args ...interface{}) {
defaultLogger.Log(DebugLevel, msg, args...)
}
func _info(args ...interface{}) {
defaultLogger.Log(InfoLevel, "", args...)
}
func _infof(msg string, args ...interface{}) {
defaultLogger.Log(InfoLevel, msg, args...)
}
func _warn(args ...interface{}) {
defaultLogger.Log(WarnLevel, "", args...)
}
func _warnf(msg string, args ...interface{}) {
defaultLogger.Log(WarnLevel, msg, args...)
}
func _error(args ...interface{}) {
defaultLogger.Log(ErrorLevel, "", args...)
}
func _errorf(msg string, args ...interface{}) {
defaultLogger.Log(ErrorLevel, msg, args...)
}
func _crit(args ...interface{}) {
defaultLogger.Log(CritLevel, "", args...)
}
func _critf(msg string, args ...interface{}) {
defaultLogger.Log(CritLevel, msg, args...)
}