forked from golang/glog
/
klogr.go
208 lines (184 loc) · 5.42 KB
/
klogr.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Package klogr implements github.com/go-logr/logr.Logger in terms of
// k8s.io/klog.
package klogr
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/go-logr/logr"
"k8s.io/klog/v2"
"k8s.io/klog/v2/internal/serialize"
)
const (
// nameKey is used to log the `WithName` values as an additional attribute.
nameKey = "logger"
)
// Option is a functional option that reconfigures the logger created with New.
type Option func(*klogger)
// Format defines how log output is produced.
type Format string
const (
// FormatSerialize tells klogr to turn key/value pairs into text itself
// before invoking klog. Key/value pairs are sorted by key.
FormatSerialize Format = "Serialize"
// FormatKlog tells klogr to pass all text messages and key/value pairs
// directly to klog. Klog itself then serializes in a human-readable
// format and optionally passes on to a structure logging backend.
FormatKlog Format = "Klog"
)
// WithFormat selects the output format.
func WithFormat(format Format) Option {
return func(l *klogger) {
l.format = format
}
}
// New returns a logr.Logger which serializes output itself
// and writes it via klog.
//
// Deprecated: this uses a custom, out-dated output format. Use textlogger.NewLogger instead.
func New() logr.Logger {
return NewWithOptions(WithFormat(FormatSerialize))
}
// NewWithOptions returns a logr.Logger which serializes as determined
// by the WithFormat option and writes via klog. The default is
// FormatKlog.
//
// Deprecated: FormatSerialize is out-dated. For FormatKlog, use textlogger.NewLogger instead.
func NewWithOptions(options ...Option) logr.Logger {
l := klogger{
level: 0,
values: nil,
format: FormatKlog,
}
for _, option := range options {
option(&l)
}
return logr.New(&l)
}
type klogger struct {
level int
callDepth int
// hasPrefix is true if the first entry in values is the special
// nameKey key/value. Such an entry gets added and later updated in
// WithName.
hasPrefix bool
values []interface{}
format Format
}
func (l *klogger) Init(info logr.RuntimeInfo) {
l.callDepth += info.CallDepth
}
func flatten(kvList ...interface{}) string {
keys := make([]string, 0, len(kvList))
vals := make(map[string]interface{}, len(kvList))
for i := 0; i < len(kvList); i += 2 {
k, ok := kvList[i].(string)
if !ok {
panic(fmt.Sprintf("key is not a string: %s", pretty(kvList[i])))
}
var v interface{}
if i+1 < len(kvList) {
v = kvList[i+1]
}
// Only print each key once...
if _, seen := vals[k]; !seen {
keys = append(keys, k)
}
// ... with the latest value.
vals[k] = v
}
sort.Strings(keys)
buf := bytes.Buffer{}
for i, k := range keys {
v := vals[k]
if i > 0 {
buf.WriteRune(' ')
}
buf.WriteString(pretty(k))
buf.WriteString("=")
buf.WriteString(pretty(v))
}
return buf.String()
}
func pretty(value interface{}) string {
if err, ok := value.(error); ok {
if _, ok := value.(json.Marshaler); !ok {
value = err.Error()
}
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
if err := encoder.Encode(value); err != nil {
return fmt.Sprintf("<<error: %v>>", err)
}
return strings.TrimSpace(buffer.String())
}
func (l *klogger) Info(level int, msg string, kvList ...interface{}) {
switch l.format {
case FormatSerialize:
msgStr := flatten("msg", msg)
merged := serialize.MergeKVs(l.values, kvList)
kvStr := flatten(merged...)
klog.VDepth(l.callDepth+1, klog.Level(level)).InfoDepth(l.callDepth+1, msgStr, " ", kvStr)
case FormatKlog:
merged := serialize.MergeKVs(l.values, kvList)
klog.VDepth(l.callDepth+1, klog.Level(level)).InfoSDepth(l.callDepth+1, msg, merged...)
}
}
func (l *klogger) Enabled(level int) bool {
return klog.VDepth(l.callDepth+1, klog.Level(level)).Enabled()
}
func (l *klogger) Error(err error, msg string, kvList ...interface{}) {
msgStr := flatten("msg", msg)
var loggableErr interface{}
if err != nil {
loggableErr = serialize.ErrorToString(err)
}
switch l.format {
case FormatSerialize:
errStr := flatten("error", loggableErr)
merged := serialize.MergeKVs(l.values, kvList)
kvStr := flatten(merged...)
klog.ErrorDepth(l.callDepth+1, msgStr, " ", errStr, " ", kvStr)
case FormatKlog:
merged := serialize.MergeKVs(l.values, kvList)
klog.ErrorSDepth(l.callDepth+1, err, msg, merged...)
}
}
// WithName returns a new logr.Logger with the specified name appended. klogr
// uses '.' characters to separate name elements. Callers should not pass '.'
// in the provided name string, but this library does not actually enforce that.
func (l klogger) WithName(name string) logr.LogSink {
if l.hasPrefix {
// Copy slice and modify value. No length checks and type
// assertions are needed because hasPrefix is only true if the
// first two elements exist and are key/value strings.
v := make([]interface{}, 0, len(l.values))
v = append(v, l.values...)
prefix, _ := v[1].(string)
prefix = prefix + "." + name
v[1] = prefix
l.values = v
} else {
// Preprend new key/value pair.
v := make([]interface{}, 0, 2+len(l.values))
v = append(v, nameKey, name)
v = append(v, l.values...)
l.values = v
l.hasPrefix = true
}
return &l
}
func (l klogger) WithValues(kvList ...interface{}) logr.LogSink {
l.values = serialize.WithValues(l.values, kvList)
return &l
}
func (l klogger) WithCallDepth(depth int) logr.LogSink {
l.callDepth += depth
return &l
}
var _ logr.LogSink = &klogger{}
var _ logr.CallDepthLogSink = &klogger{}