forked from mailgun/groupcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
155 lines (125 loc) · 2.88 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package groupcache
import (
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
)
// Logger is a minimal interface that will allow us to use structured loggers,
// including (but not limited to) logrus.
type Logger interface {
// Error logging level
Error() Logger
// Warn logging level
Warn() Logger
// Info logging level
Info() Logger
// Debug logging level
Debug() Logger
// ErrorField is a field with an error value
ErrorField(label string, err error) Logger
// StringField is a field with a string value
StringField(label string, val string) Logger
WithFields(fields map[string]interface{}) Logger
// Printf is called last to emit the log at the given
// level.
Printf(format string, args ...interface{})
}
// LogrusLogger is an implementation of Logger that wraps logrus... who knew?
type LogrusLogger struct {
Entry *logrus.Entry
level logrus.Level
}
func (l LogrusLogger) Info() Logger {
return LogrusLogger{
Entry: l.Entry,
level: logrus.InfoLevel,
}
}
func (l LogrusLogger) Debug() Logger {
return LogrusLogger{
Entry: l.Entry,
level: logrus.DebugLevel,
}
}
func (l LogrusLogger) Warn() Logger {
return LogrusLogger{
Entry: l.Entry,
level: logrus.WarnLevel,
}
}
func (l LogrusLogger) Error() Logger {
return LogrusLogger{
Entry: l.Entry,
level: logrus.ErrorLevel,
}
}
func (l LogrusLogger) WithFields(fields map[string]interface{}) Logger {
return LogrusLogger{
Entry: l.Entry.WithFields(fields),
level: l.level,
}
}
// ErrorField - create a field for an error
func (l LogrusLogger) ErrorField(label string, err error) Logger {
return LogrusLogger{
Entry: l.Entry.WithField(label, err),
level: l.level,
}
}
// StringField - create a field for a string.
func (l LogrusLogger) StringField(label string, val string) Logger {
return LogrusLogger{
Entry: l.Entry.WithField(label, val),
level: l.level,
}
}
func (l LogrusLogger) Printf(format string, args ...interface{}) {
l.Entry.Logf(l.level, format, args...)
}
func NewZeroLogger(l zerolog.Logger) Logger {
return ZeroLogger{
l: l,
}
}
type ZeroLogger struct {
l zerolog.Logger
e *zerolog.Event
}
func (zl ZeroLogger) Error() Logger {
return ZeroLogger{
l: zl.l,
e: zl.l.Error(),
}
}
func (zl ZeroLogger) Warn() Logger {
return ZeroLogger{
l: zl.l,
e: zl.l.Warn(),
}
}
func (zl ZeroLogger) Info() Logger {
return ZeroLogger{
l: zl.l,
e: zl.l.Info(),
}
}
func (zl ZeroLogger) Debug() Logger {
return ZeroLogger{
l: zl.l,
e: zl.l.Debug(),
}
}
func (zl ZeroLogger) ErrorField(label string, err error) Logger {
zl.e = zl.e.AnErr(label, err)
return zl
}
func (zl ZeroLogger) StringField(label string, val string) Logger {
zl.e = zl.e.Str(label, val)
return zl
}
func (zl ZeroLogger) WithFields(fields map[string]interface{}) Logger {
zl.e = zl.e.Fields(fields)
return zl
}
func (zl ZeroLogger) Printf(format string, args ...interface{}) {
zl.e.Msgf(format, args...)
}