-
Notifications
You must be signed in to change notification settings - Fork 88
/
log.go
58 lines (48 loc) · 1.06 KB
/
log.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
package turnpike
import (
glog "log"
"os"
)
var (
logFlags = glog.Ldate | glog.Ltime | glog.Lshortfile
log Logger
)
// Logger is an interface compatible with log.Logger.
type Logger interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
type noopLogger struct{}
func (n noopLogger) Println(v ...interface{}) {}
func (n noopLogger) Printf(format string, v ...interface{}) {}
// setup logger for package, noop by default
func init() {
if os.Getenv("DEBUG") != "" {
log = glog.New(os.Stderr, "", logFlags)
} else {
log = noopLogger{}
}
}
// Debug changes the log output to stderr
func Debug() {
log = glog.New(os.Stderr, "", logFlags)
}
// DebugOff changes the log to a noop logger
func DebugOff() {
log = noopLogger{}
}
// SetLogger allows users to inject their own logger instead of the default one.
func SetLogger(l Logger) {
log = l
}
func logErr(err error) error {
if err == nil {
return nil
}
if l, ok := log.(*glog.Logger); ok {
l.Output(2, err.Error())
} else {
log.Println(err)
}
return err
}