-
Notifications
You must be signed in to change notification settings - Fork 3
/
logr.go
61 lines (51 loc) · 1.38 KB
/
logr.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
package micrologger
import (
"context"
"strings"
kitlog "github.com/go-kit/log"
"github.com/go-logr/logr"
"github.com/go-stack/stack"
)
type LogrSink struct {
*MicroLogger
}
func (l *LogrSink) Init(info logr.RuntimeInfo) {
l.info = info
}
func (l *LogrSink) Enabled(level int) bool {
return l.verbosity >= level
}
func (l *LogrSink) Info(level int, msg string, keysAndValues ...interface{}) {
if l.verbosity < level {
return
}
keysAndValues = append(keysAndValues, l.getValues("debug")...)
l.With(keysAndValues...).Log("message", msg)
}
func (l *LogrSink) Error(err error, msg string, keysAndValues ...interface{}) {
if l.verbosity < 1 {
return
}
keysAndValues = append(keysAndValues, l.getValues("error")...)
l.With(keysAndValues...).Errorf(context.Background(), err, msg)
}
func (l *LogrSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
loggerCopy := l.deepCopy()
loggerCopy.logger = kitlog.With(loggerCopy.logger, processStack(keysAndValues)...)
return loggerCopy.AsSink(loggerCopy.verbosity)
}
func (l *LogrSink) WithName(name string) logr.LogSink {
loggerCopy := l.deepCopy()
loggerCopy.names = append(loggerCopy.names[:], name)
return loggerCopy.AsSink(l.verbosity)
}
func (l *LogrSink) getValues(level string) []interface{} {
return []interface{}{
"level",
level,
"name",
strings.Join(l.names, "."),
"caller",
stack.Caller(l.info.CallDepth),
}
}