-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_adapter.go
111 lines (98 loc) · 2.81 KB
/
log_adapter.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
package k8s
import (
"strings"
"github.com/alibaba/sentinel-golang/logging"
"github.com/go-logr/logr"
)
// noopLogger is a logr.Logger that's always disabled, and does nothing.
type noopLogger struct{}
func (l *noopLogger) Enabled() bool { return false }
func (l *noopLogger) Info(_ string, _ ...interface{}) {}
func (l *noopLogger) Error(_ error, _ string, _ ...interface{}) {}
func (l *noopLogger) V(_ int) logr.Logger { return l }
func (l *noopLogger) WithValues(_ ...interface{}) logr.Logger { return l }
func (l *noopLogger) WithName(_ string) logr.Logger { return l }
var disabledLogger = &noopLogger{}
type k8SLogger struct {
l logging.Logger
level logging.Level
names []string
keysAndValues []interface{}
}
func (k *k8SLogger) buildNames() string {
size := len(k.names)
if size == 0 {
return ""
}
sb := strings.Builder{}
for i, name := range k.names {
sb.WriteString(name)
if i == size-1 {
continue
}
sb.WriteString(".")
}
sb.WriteString(" ")
return sb.String()
}
func (k *k8SLogger) Info(msg string, keysAndValues ...interface{}) {
keysAndValues = append(keysAndValues, k.keysAndValues...)
switch k.level {
case logging.WarnLevel:
k.l.Warn(k.buildNames()+msg, keysAndValues...)
case logging.InfoLevel:
k.l.Info(k.buildNames()+msg, keysAndValues...)
case logging.DebugLevel:
k.l.Debug(k.buildNames()+msg, keysAndValues...)
default:
k.l.Info(k.buildNames()+msg, keysAndValues...)
}
}
func (k *k8SLogger) Enabled() bool {
return true
}
func (k *k8SLogger) Error(err error, msg string, keysAndValues ...interface{}) {
keysAndValues = append(keysAndValues, k.keysAndValues...)
k.l.Error(err, k.buildNames()+msg, keysAndValues...)
}
func (k *k8SLogger) V(level int) logr.Logger {
if k.Enabled() {
names := make([]string, len(k.names))
copy(names, k.names)
kvs := make([]interface{}, len(k.keysAndValues))
copy(kvs, k.keysAndValues)
return &k8SLogger{
l: k.l,
level: logging.Level(level),
names: names,
keysAndValues: kvs,
}
}
return disabledLogger
}
func (k *k8SLogger) WithValues(keysAndValues ...interface{}) logr.Logger {
names := make([]string, len(k.names))
copy(names, k.names)
kvs := make([]interface{}, len(k.keysAndValues))
copy(kvs, k.keysAndValues)
kvs = append(kvs, keysAndValues...)
return &k8SLogger{
l: k.l,
level: k.level,
names: names,
keysAndValues: kvs,
}
}
func (k *k8SLogger) WithName(name string) logr.Logger {
names := make([]string, len(k.names))
copy(names, k.names)
names = append(names, name)
kvs := make([]interface{}, len(k.keysAndValues))
copy(kvs, k.keysAndValues)
return &k8SLogger{
l: k.l,
level: k.level,
names: names,
keysAndValues: kvs,
}
}