-
Notifications
You must be signed in to change notification settings - Fork 3
/
logrus.go
91 lines (86 loc) · 2.27 KB
/
logrus.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
package sched
import (
"github.com/sirupsen/logrus"
)
type lruLogger struct {
jl *logrus.Entry
}
func (l *lruLogger) Debugw(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Debug(msg)
}
func (l lruLogger) Errorw(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Error(msg)
}
func (l lruLogger) Fatalw(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Fatal(msg)
}
func (l lruLogger) Infow(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Info(msg)
}
func (l lruLogger) Panicw(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Panic(msg)
}
func (l lruLogger) Warnw(msg string, keysAndValues ...interface{}) {
logger := l.jl
for i := 0; i < len(keysAndValues); i++ {
if i%2 == 0 {
logger = logger.WithField(keysAndValues[i].(string), keysAndValues[i+1])
}
}
logger.Warn(msg)
}
func (l *lruLogger) With(args ...interface{}) Logger {
for i := 0; i < len(args); i++ {
if i%2 == 0 {
l.jl = l.jl.WithField(args[i].(string), args[i+1])
}
}
return l
}
func (l lruLogger) Named(name string) Logger {
logger := l.jl.WithField("From", name)
return &lruLogger{jl: logger}
}
func (l *lruLogger) Sync() error {
return nil
}
//LogrusDefaultLogger Return Logger based on logrus with new instance
func LogrusDefaultLogger() Logger {
// TODO control verbosity
return &lruLogger{jl: logrus.NewEntry(logrus.New())}
}
//LogrusLogger Return Return Logger based on logrus with existing instance
func LogrusLogger(log *logrus.Logger) Logger {
// TODO control verbosity
return &lruLogger{jl: logrus.NewEntry(log)}
}