-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
67 lines (58 loc) · 1.37 KB
/
logging.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
package bcr
import (
"log"
"go.uber.org/zap"
)
// LogFunc is a function used for logging
type LogFunc func(template string, args ...interface{})
// Logger is a basic logger
type Logger struct {
Debug LogFunc
Info LogFunc
Error LogFunc
}
// NewNoopLogger returns a Logger that does nothing when its functions are called
func NewNoopLogger() *Logger {
return &Logger{
Debug: func(t string, args ...interface{}) {
return
},
Info: func(t string, args ...interface{}) {
return
},
Error: func(t string, args ...interface{}) {
return
},
}
}
// wrapStdLog returns a function that calls log.Printf with the given prefix
func wrapStdLog(prefix string) LogFunc {
return func(template string, args ...interface{}) {
log.Printf(prefix+": "+template, args...)
}
}
// NewStdlibLogger returns a Logger that wraps the standard library's "log" package
func NewStdlibLogger(debug bool) *Logger {
if debug {
return &Logger{
Debug: wrapStdLog("DEBUG"),
Info: wrapStdLog("INFO"),
Error: wrapStdLog("ERROR"),
}
}
return &Logger{
Debug: func(t string, args ...interface{}) {
return
},
Info: wrapStdLog("INFO"),
Error: wrapStdLog("ERROR"),
}
}
// NewZapLogger returns a Logger that wraps a SugaredLogger from go.uber.org/zap
func NewZapLogger(s *zap.SugaredLogger) *Logger {
return &Logger{
Debug: s.Debugf,
Info: s.Infof,
Error: s.Errorf,
}
}