-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathlog.go
74 lines (62 loc) · 1.22 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package core
import (
"os"
"strings"
"github.com/sirupsen/logrus"
)
const (
DebugEnvVar = "DEBUG_GOVPP"
debugOptMsgId = "msgid"
debugOptChannels = "channels"
)
var (
debugOn = os.Getenv(DebugEnvVar) != ""
debugMap = initDebugMap(os.Getenv(DebugEnvVar))
log = logrus.New()
)
// init initializes global logger
func init() {
log.Formatter = &logrus.TextFormatter{
EnvironmentOverrideColors: true,
}
if debugOn {
log.Level = logrus.DebugLevel
log.Debugf("govpp: debug enabled %+v", debugMap)
}
}
func isDebugOn(u string) bool {
_, ok := debugMap[u]
return ok
}
func initDebugMap(s string) map[string]string {
debugMap := make(map[string]string)
for _, p := range splitString(s) {
var key, val string
kv := strings.SplitN(p, "=", 2)
key = kv[0]
if len(kv) > 1 {
val = kv[1]
} else {
val = "true"
}
debugMap[key] = val
}
return debugMap
}
func splitString(s string) []string {
return strings.FieldsFunc(s, func(c rune) bool {
switch c {
case ';', ',', ' ':
return true
}
return false
})
}
// SetLogger sets global logger to l.
func SetLogger(l *logrus.Logger) {
log = l
}
// SetLogLevel sets global logger level to lvl.
func SetLogLevel(lvl logrus.Level) {
log.Level = lvl
}