-
Notifications
You must be signed in to change notification settings - Fork 149
/
logger.go
118 lines (97 loc) · 2.52 KB
/
logger.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
package logging
import (
"os"
gologging "github.com/op/go-logging"
)
const (
defaultModule = "default"
)
var (
backends []gologging.Backend
Log Logger
format gologging.Formatter
)
func init() {
Log = Logger{
log: gologging.MustGetLogger(defaultModule),
}
Log.log.ExtraCalldepth = 1
format = gologging.MustStringFormatter("%{time:2006-01-02T15:04:05.000000Z} %{shortfunc:.6s} %{level}: %{message}")
backend := gologging.NewLogBackend(os.Stdout, "", 0)
backendFormatter := gologging.NewBackendFormatter(backend, format)
leveledBackend := gologging.AddModuleLevel(backendFormatter)
backends = append(backends, leveledBackend)
gologging.SetBackend(backends...)
SetLevel(SILENT)
}
func SetSysLogger(syslog string) {
syslogBackend, err := gologging.NewSyslogBackend(syslog)
if err != nil {
panic(err)
}
backendFormatter := gologging.NewBackendFormatter(syslogBackend, format)
syslogLeveledBackend := gologging.AddModuleLevel(backendFormatter)
backends = append(backends, syslogLeveledBackend)
gologging.SetBackend(backends...)
}
type Logger struct {
log *gologging.Logger
}
func SetLevel(level LogLevel) {
for _, backend := range backends {
leveled := backend.(gologging.LeveledBackend)
leveled.SetLevel(convertLogLevel(level), "")
}
}
func (l Logger) Error(msg string, err error) {
// NOTICE: go vet doesn't like the following line:
// l.log.Error("%s : %v", msg, err)
// Therefore:
l.log.Error(""+"%s : %v", msg, err)
}
func (l Logger) Errorf(msg string, values ...interface{}) {
l.log.Errorf(msg, values...)
}
func (l Logger) Info(msg string) {
l.log.Info(msg)
}
func (l Logger) Infof(msg string, values ...interface{}) {
l.log.Infof(msg, values...)
}
func (l Logger) Debug(msg string) {
l.log.Debug(msg)
}
func (l Logger) Debugf(msg string, values ...interface{}) {
l.log.Debugf(msg, values...)
}
func (l Logger) Warning(msg string) {
l.log.Warning(msg)
}
func (l Logger) Warningf(msg string, values ...interface{}) {
l.log.Warningf(msg, values...)
}
func (l Logger) Panic(msg string, err error) {
l.log.Panicf("%s : %v", msg, err)
}
func (l Logger) Panicf(msg string, values ...interface{}) {
l.log.Panicf(msg, values...)
}
func convertLogLevel(level LogLevel) gologging.Level {
switch level {
case SILENT:
return -1
case FATAL:
return gologging.CRITICAL
case ERROR:
return gologging.ERROR
case WARNING:
return gologging.WARNING
case INFO:
return gologging.INFO
case DEBUG:
return gologging.DEBUG
default:
Log.Panicf("Unknown LogLevel: %v", level)
return gologging.CRITICAL
}
}