-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.go
81 lines (69 loc) · 2.04 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
package slog
import (
"fmt"
"time"
)
var root logger
func makeRoot(now fmt.Stringer) logger {
root := logger{
defaultTarget: stdout,
context: map[string]interface{}{
"$time": now,
},
}
root.genLCache(nil)
root.genTCache(nil)
return root
}
type currentTime struct{}
func (_ currentTime) String() string {
// TODO(carl): Just hardcode this. It'll be faster and we can even make
// it fixed-width.
return time.Now().UTC().Format(time.RFC3339Nano)
}
func init() {
root = makeRoot(currentTime{})
}
var DefaultLevel = LInfo
// String implements the fmt.Stringer interface by returning one of DEBUG, INFO,
// WARN, or ERROR.
func (l Level) String() string {
switch l {
case LDebug:
return "DEBUG"
case LInfo:
return "INFO"
case LWarn:
return "WARN"
case LError:
return "ERROR"
default:
// Unclear how this would happen, but it's probably not nice to
// panic().
return fmt.Sprintf("Level(%d)", l)
}
}
// Bind returns a new Logger forked from the global root logger that
// additionally binds the given context variables. It is the only way to create
// new Loggers, therefore all Loggers, regardless of where they are created have
// a single common root.
func Bind(context map[string]interface{}) Logger {
return root.Bind(context)
}
// SetLevel sets the log level for a given selector on the root logger. See the
// documentation for Logger.SetLevel for the syntax accepted for the selector.
func SetLevel(selector string, level Level) {
root.SetLevel(selector, level)
}
// LogTo logs pre-formatted log lines at the given levels to a channel. If you
// do not pass any levels, the channel will be used as the default logger for
// levels not otherwise configured.
func LogTo(target chan<- string, levels ...Level) {
root.LogTo(target, levels...)
}
// SlogTo logs raw log lines at the given levels to a channel. If you do not
// pass any levels, the channel will be used as the default logger for levels
// not otherwise configured.
func SlogTo(target chan<- map[string]interface{}, levels ...Level) {
root.SlogTo(target, levels...)
}