-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
73 lines (57 loc) · 1.3 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
package ble
import (
"os"
"sync"
"github.com/sirupsen/logrus"
)
type Logger interface {
Info(...interface{})
Debug(...interface{})
Error(...interface{})
Warn(...interface{})
Infof(string, ...interface{})
Debugf(string, ...interface{})
Errorf(string, ...interface{})
Warnf(string, ...interface{})
ChildLogger(tags map[string]interface{}) Logger
}
var logger Logger
var loggerMu sync.Mutex
func SetLogLevelMax() {
l := GetLogger()
if lg, ok := l.(*defaultLogger); ok {
lg.Entry.Logger.SetLevel(logrus.TraceLevel)
} else {
// clean this up later
l.Error("non-default logger, don't know how to set level")
}
}
func SetLogger(l Logger) {
loggerMu.Lock()
defer loggerMu.Unlock()
logger = l
}
func GetLogger() Logger {
loggerMu.Lock()
defer loggerMu.Unlock()
if logger == nil {
logger = buildDefaultLogger()
}
return logger
}
type defaultLogger struct {
*logrus.Entry
}
func buildDefaultLogger() Logger {
l := &logrus.Logger{
Formatter: &logrus.TextFormatter{DisableTimestamp: true},
Level: logrus.InfoLevel,
Out: os.Stderr,
Hooks: make(logrus.LevelHooks),
}
return &defaultLogger{Entry: l.WithFields(map[string]interface{}{})}
}
func (d *defaultLogger) ChildLogger(ff map[string]interface{}) Logger {
nl := &defaultLogger{d.Entry.WithFields(ff)}
return nl
}