Skip to content

Commit 4171f3c

Browse files
committedAug 3, 2021
Switching to logr tag v1.0.0
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
1 parent 79f9f6b commit 4171f3c

File tree

7 files changed

+117
-140
lines changed

7 files changed

+117
-140
lines changed
 

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module k8s.io/klog/v2
22

33
go 1.13
44

5-
require github.com/go-logr/logr v0.4.0
5+
require github.com/go-logr/logr v1.0.0

‎go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
2-
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
1+
github.com/go-logr/logr v1.0.0-rc1 h1:+ul9F74rBkPajeP8m4o3o0tiglmzNFsPnuhYyBCQ0Sc=
2+
github.com/go-logr/logr v1.0.0-rc1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
3+
github.com/go-logr/logr v1.0.0 h1:kH951GinvFVaQgy/ki/B3YYmQtRpExGigSJg6O8z5jo=
4+
github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=

‎klog.go

+37-29
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ type loggingT struct {
509509
addDirHeader bool
510510

511511
// If set, all output will be redirected unconditionally to the provided logr.Logger
512-
logr logr.Logger
512+
logr *logr.Logger
513513

514514
// If true, messages will not be propagated to lower severity log levels
515515
oneOutput bool
@@ -698,30 +698,30 @@ func (buf *buffer) someDigits(i, d int) int {
698698
return copy(buf.tmp[i:], buf.tmp[j:])
699699
}
700700

701-
func (l *loggingT) println(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
701+
func (l *loggingT) println(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
702702
buf, file, line := l.header(s, 0)
703-
// if logr is set, we clear the generated header as we rely on the backing
704-
// logr implementation to print headers
705-
if logr != nil {
703+
// if logger is set, we clear the generated header as we rely on the backing
704+
// logger implementation to print headers
705+
if logger != nil {
706706
l.putBuffer(buf)
707707
buf = l.getBuffer()
708708
}
709709
if filter != nil {
710710
args = filter.Filter(args)
711711
}
712712
fmt.Fprintln(buf, args...)
713-
l.output(s, logr, buf, 0 /* depth */, file, line, false)
713+
l.output(s, logger, buf, 0 /* depth */, file, line, false)
714714
}
715715

716-
func (l *loggingT) print(s severity, logr logr.Logger, filter LogFilter, args ...interface{}) {
717-
l.printDepth(s, logr, filter, 1, args...)
716+
func (l *loggingT) print(s severity, logger *logr.Logger, filter LogFilter, args ...interface{}) {
717+
l.printDepth(s, logger, filter, 1, args...)
718718
}
719719

720-
func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, depth int, args ...interface{}) {
720+
func (l *loggingT) printDepth(s severity, logger *logr.Logger, filter LogFilter, depth int, args ...interface{}) {
721721
buf, file, line := l.header(s, depth)
722722
// if logr is set, we clear the generated header as we rely on the backing
723723
// logr implementation to print headers
724-
if logr != nil {
724+
if logger != nil {
725725
l.putBuffer(buf)
726726
buf = l.getBuffer()
727727
}
@@ -732,14 +732,14 @@ func (l *loggingT) printDepth(s severity, logr logr.Logger, filter LogFilter, de
732732
if buf.Bytes()[buf.Len()-1] != '\n' {
733733
buf.WriteByte('\n')
734734
}
735-
l.output(s, logr, buf, depth, file, line, false)
735+
l.output(s, logger, buf, depth, file, line, false)
736736
}
737737

738-
func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format string, args ...interface{}) {
738+
func (l *loggingT) printf(s severity, logger *logr.Logger, filter LogFilter, format string, args ...interface{}) {
739739
buf, file, line := l.header(s, 0)
740740
// if logr is set, we clear the generated header as we rely on the backing
741741
// logr implementation to print headers
742-
if logr != nil {
742+
if logger != nil {
743743
l.putBuffer(buf)
744744
buf = l.getBuffer()
745745
}
@@ -750,17 +750,17 @@ func (l *loggingT) printf(s severity, logr logr.Logger, filter LogFilter, format
750750
if buf.Bytes()[buf.Len()-1] != '\n' {
751751
buf.WriteByte('\n')
752752
}
753-
l.output(s, logr, buf, 0 /* depth */, file, line, false)
753+
l.output(s, logger, buf, 0 /* depth */, file, line, false)
754754
}
755755

756756
// printWithFileLine behaves like print but uses the provided file and line number. If
757757
// alsoLogToStderr is true, the log message always appears on standard error; it
758758
// will also appear in the log file unless --logtostderr is set.
759-
func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
759+
func (l *loggingT) printWithFileLine(s severity, logger *logr.Logger, filter LogFilter, file string, line int, alsoToStderr bool, args ...interface{}) {
760760
buf := l.formatHeader(s, file, line)
761761
// if logr is set, we clear the generated header as we rely on the backing
762762
// logr implementation to print headers
763-
if logr != nil {
763+
if logger != nil {
764764
l.putBuffer(buf)
765765
buf = l.getBuffer()
766766
}
@@ -771,28 +771,28 @@ func (l *loggingT) printWithFileLine(s severity, logr logr.Logger, filter LogFil
771771
if buf.Bytes()[buf.Len()-1] != '\n' {
772772
buf.WriteByte('\n')
773773
}
774-
l.output(s, logr, buf, 2 /* depth */, file, line, alsoToStderr)
774+
l.output(s, logger, buf, 2 /* depth */, file, line, alsoToStderr)
775775
}
776776

777777
// if loggr is specified, will call loggr.Error, otherwise output with logging module.
778-
func (l *loggingT) errorS(err error, loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
778+
func (l *loggingT) errorS(err error, logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
779779
if filter != nil {
780780
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
781781
}
782-
if loggr != nil {
783-
logr.WithCallDepth(loggr, depth+2).Error(err, msg, keysAndValues...)
782+
if logger != nil {
783+
logger.WithCallDepth(depth+2).Error(err, msg, keysAndValues...)
784784
return
785785
}
786786
l.printS(err, errorLog, depth+1, msg, keysAndValues...)
787787
}
788788

789789
// if loggr is specified, will call loggr.Info, otherwise output with logging module.
790-
func (l *loggingT) infoS(loggr logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
790+
func (l *loggingT) infoS(logger *logr.Logger, filter LogFilter, depth int, msg string, keysAndValues ...interface{}) {
791791
if filter != nil {
792792
msg, keysAndValues = filter.FilterS(msg, keysAndValues)
793793
}
794-
if loggr != nil {
795-
logr.WithCallDepth(loggr, depth+2).Info(msg, keysAndValues...)
794+
if logger != nil {
795+
logger.WithCallDepth(depth+2).Info(msg, keysAndValues...)
796796
return
797797
}
798798
l.printS(nil, infoLog, depth+1, msg, keysAndValues...)
@@ -866,7 +866,14 @@ func SetLogger(logr logr.Logger) {
866866
logging.mu.Lock()
867867
defer logging.mu.Unlock()
868868

869-
logging.logr = logr
869+
logging.logr = &logr
870+
}
871+
872+
func clearLogger() {
873+
logging.mu.Lock()
874+
defer logging.mu.Unlock()
875+
876+
logging.logr = nil
870877
}
871878

872879
// SetOutput sets the output destination for all severities
@@ -904,7 +911,7 @@ func LogToStderr(stderr bool) {
904911
}
905912

906913
// output writes the data to the log files and releases the buffer.
907-
func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
914+
func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int, file string, line int, alsoToStderr bool) {
908915
l.mu.Lock()
909916
if l.traceLocation.isSet() {
910917
if l.traceLocation.match(file, line) {
@@ -916,9 +923,9 @@ func (l *loggingT) output(s severity, log logr.Logger, buf *buffer, depth int, f
916923
// TODO: set 'severity' and caller information as structured log info
917924
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
918925
if s == errorLog {
919-
logr.WithCallDepth(l.logr, depth+3).Error(nil, string(data))
926+
l.logr.WithCallDepth(depth+3).Error(nil, string(data))
920927
} else {
921-
logr.WithCallDepth(log, depth+3).Info(string(data))
928+
log.WithCallDepth(depth + 3).Info(string(data))
922929
}
923930
} else if l.toStderr {
924931
os.Stderr.Write(data)
@@ -1269,15 +1276,16 @@ func (l *loggingT) setV(pc uintptr) Level {
12691276
// See the documentation of V for more information.
12701277
type Verbose struct {
12711278
enabled bool
1272-
logr logr.Logger
1279+
logr *logr.Logger
12731280
filter LogFilter
12741281
}
12751282

12761283
func newVerbose(level Level, b bool) Verbose {
12771284
if logging.logr == nil {
12781285
return Verbose{b, nil, logging.filter}
12791286
}
1280-
return Verbose{b, logging.logr.V(int(level)), logging.filter}
1287+
v := logging.logr.V(int(level))
1288+
return Verbose{b, &v, logging.filter}
12811289
}
12821290

12831291
// V reports whether verbosity at the call site is at least the requested level.

‎klog_test.go

+33-21
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func testVmoduleGlob(pat string, match bool, t *testing.T) {
446446
defer logging.vmodule.Set("")
447447
logging.vmodule.Set(pat)
448448
if V(2).Enabled() != match {
449-
t.Errorf("incorrect match for %q: got %t expected %t", pat, V(2), match)
449+
t.Errorf("incorrect match for %q: got %#v expected %#v", pat, V(2), match)
450450
}
451451
}
452452

@@ -1375,8 +1375,9 @@ func TestInfoSWithLogr(t *testing.T) {
13751375

13761376
for _, data := range testDataInfo {
13771377
t.Run(data.msg, func(t *testing.T) {
1378-
SetLogger(logger)
1379-
defer SetLogger(nil)
1378+
l := logr.New(logger)
1379+
SetLogger(l)
1380+
defer clearLogger()
13801381
defer logger.reset()
13811382

13821383
InfoS(data.msg, data.keysValues...)
@@ -1442,8 +1443,9 @@ func TestErrorSWithLogr(t *testing.T) {
14421443

14431444
for _, data := range testDataInfo {
14441445
t.Run(data.msg, func(t *testing.T) {
1445-
SetLogger(logger)
1446-
defer SetLogger(nil)
1446+
l := logr.New(logger)
1447+
SetLogger(l)
1448+
defer clearLogger()
14471449
defer logger.reset()
14481450

14491451
ErrorS(data.err, data.msg, data.keysValues...)
@@ -1499,8 +1501,9 @@ func TestCallDepthLogr(t *testing.T) {
14991501

15001502
for _, tc := range testCases {
15011503
t.Run(tc.name, func(t *testing.T) {
1502-
SetLogger(logger)
1503-
defer SetLogger(nil)
1504+
l := logr.New(logger)
1505+
SetLogger(l)
1506+
defer clearLogger()
15041507
defer logger.reset()
15051508
defer logger.resetCallDepth()
15061509

@@ -1520,7 +1523,8 @@ func TestCallDepthLogr(t *testing.T) {
15201523
func TestCallDepthLogrInfoS(t *testing.T) {
15211524
logger := &callDepthTestLogr{}
15221525
logger.resetCallDepth()
1523-
SetLogger(logger)
1526+
l := logr.New(logger)
1527+
SetLogger(l)
15241528

15251529
// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
15261530
logFunc := func() {
@@ -1541,7 +1545,8 @@ func TestCallDepthLogrInfoS(t *testing.T) {
15411545
func TestCallDepthLogrErrorS(t *testing.T) {
15421546
logger := &callDepthTestLogr{}
15431547
logger.resetCallDepth()
1544-
SetLogger(logger)
1548+
l := logr.New(logger)
1549+
SetLogger(l)
15451550

15461551
// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
15471552
logFunc := func() {
@@ -1562,7 +1567,8 @@ func TestCallDepthLogrErrorS(t *testing.T) {
15621567
func TestCallDepthLogrGoLog(t *testing.T) {
15631568
logger := &callDepthTestLogr{}
15641569
logger.resetCallDepth()
1565-
SetLogger(logger)
1570+
l := logr.New(logger)
1571+
SetLogger(l)
15661572
CopyStandardLogTo("INFO")
15671573

15681574
// Add wrapper to ensure callDepthTestLogr +2 offset is correct.
@@ -1588,7 +1594,7 @@ func TestCallDepthTestLogr(t *testing.T) {
15881594
logger.resetCallDepth()
15891595

15901596
logFunc := func() {
1591-
logger.Info("some info log")
1597+
logger.Info(0, "some info log")
15921598
}
15931599
// Keep these lines together.
15941600
_, wantFile, wantLine, _ := runtime.Caller(0)
@@ -1634,7 +1640,7 @@ func (l *testLogr) reset() {
16341640
l.entries = []testLogrEntry{}
16351641
}
16361642

1637-
func (l *testLogr) Info(msg string, keysAndValues ...interface{}) {
1643+
func (l *testLogr) Info(level int, msg string, keysAndValues ...interface{}) {
16381644
l.mutex.Lock()
16391645
defer l.mutex.Unlock()
16401646
l.entries = append(l.entries, testLogrEntry{
@@ -1655,12 +1661,15 @@ func (l *testLogr) Error(err error, msg string, keysAndValues ...interface{}) {
16551661
})
16561662
}
16571663

1658-
func (l *testLogr) Enabled() bool { panic("not implemented") }
1659-
func (l *testLogr) V(int) logr.Logger { panic("not implemented") }
1660-
func (l *testLogr) WithName(string) logr.Logger { panic("not implemented") }
1661-
func (l *testLogr) WithValues(...interface{}) logr.Logger {
1662-
panic("not implemented")
1663-
}
1664+
func (l *testLogr) Init(info logr.RuntimeInfo) {}
1665+
func (l *testLogr) Enabled(level int) bool { return true }
1666+
func (l *testLogr) V(int) logr.Logger { panic("not implemented") }
1667+
func (l *testLogr) WithName(string) logr.LogSink { panic("not implemented") }
1668+
func (l *testLogr) WithValues(...interface{}) logr.LogSink { panic("not implemented") }
1669+
func (l *testLogr) WithCallDepth(depth int) logr.LogSink { return l }
1670+
1671+
var _ logr.LogSink = &testLogr{}
1672+
var _ logr.CallDepthLogSink = &testLogr{}
16641673

16651674
type callDepthTestLogr struct {
16661675
testLogr
@@ -1673,17 +1682,17 @@ func (l *callDepthTestLogr) resetCallDepth() {
16731682
l.callDepth = 0
16741683
}
16751684

1676-
func (l *callDepthTestLogr) WithCallDepth(depth int) logr.Logger {
1685+
func (l *callDepthTestLogr) WithCallDepth(depth int) logr.LogSink {
16771686
l.mutex.Lock()
16781687
defer l.mutex.Unlock()
16791688
// Note: Usually WithCallDepth would be implemented by cloning l
16801689
// and setting the call depth on the clone. We modify l instead in
16811690
// this test helper for simplicity.
1682-
l.callDepth = depth
1691+
l.callDepth = depth + 1
16831692
return l
16841693
}
16851694

1686-
func (l *callDepthTestLogr) Info(msg string, keysAndValues ...interface{}) {
1695+
func (l *callDepthTestLogr) Info(level int, msg string, keysAndValues ...interface{}) {
16871696
l.mutex.Lock()
16881697
defer l.mutex.Unlock()
16891698
// Add 2 to depth for the wrapper function caller and for invocation in
@@ -1710,6 +1719,9 @@ func (l *callDepthTestLogr) Error(err error, msg string, keysAndValues ...interf
17101719
})
17111720
}
17121721

1722+
var _ logr.LogSink = &callDepthTestLogr{}
1723+
var _ logr.CallDepthLogSink = &callDepthTestLogr{}
1724+
17131725
func checkLogrEntryCorrectCaller(t *testing.T, wantFile string, wantLine int, entry testLogrEntry) {
17141726
t.Helper()
17151727

‎klogr/calldepth-test/call_depth_helper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
// their source code file is *not* logged because of WithCallDepth(1).
99

1010
func myInfo(l logr.Logger, msg string) {
11-
logr.WithCallDepth(l, 1).Info(msg)
11+
l.WithCallDepth(2).Info(msg)
1212
}
1313

1414
func myInfo2(l logr.Logger, msg string) {
15-
myInfo(logr.WithCallDepth(l, 1), msg)
15+
myInfo(l.WithCallDepth(2), msg)
1616
}

‎klogr/klogr.go

+36-79
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import (
66
"bytes"
77
"encoding/json"
88
"fmt"
9-
"runtime"
10-
"sort"
11-
"strings"
12-
139
"github.com/go-logr/logr"
1410
"k8s.io/klog/v2"
11+
"sort"
12+
"strings"
Has a conversation. Original line has a conversation.
1513
)
1614

1715
// Option is a functional option that reconfigures the logger created with New.
@@ -57,7 +55,7 @@ func NewWithOptions(options ...Option) logr.Logger {
5755
for _, option := range options {
5856
option(&l)
5957
}
60-
return l
58+
return logr.New(&l)
6159
}
6260

6361
type klogger struct {
@@ -68,40 +66,8 @@ type klogger struct {
6866
format Format
6967
}
7068

71-
func (l klogger) clone() klogger {
72-
return klogger{
73-
level: l.level,
74-
prefix: l.prefix,
75-
values: copySlice(l.values),
76-
format: l.format,
77-
}
78-
}
79-
80-
func copySlice(in []interface{}) []interface{} {
81-
out := make([]interface{}, len(in))
82-
copy(out, in)
83-
return out
84-
}
85-
86-
// Magic string for intermediate frames that we should ignore.
87-
const autogeneratedFrameName = "<autogenerated>"
88-
89-
// Discover how many frames we need to climb to find the caller. This approach
90-
// was suggested by Ian Lance Taylor of the Go team, so it *should* be safe
91-
// enough (famous last words).
92-
//
93-
// It is needed because binding the specific klogger functions to the
94-
// logr interface creates one additional call frame that neither we nor
95-
// our caller know about.
96-
func framesToCaller() int {
97-
// 1 is the immediate caller. 3 should be too many.
98-
for i := 1; i < 3; i++ {
99-
_, file, _, _ := runtime.Caller(i + 1) // +1 for this function's frame
100-
if file != autogeneratedFrameName {
101-
return i
102-
}
103-
}
104-
return 1 // something went wrong, this is safe
69+
func (l *klogger) Init(info logr.RuntimeInfo) {
70+
l.callDepth += info.CallDepth
10571
}
10672

10773
// trimDuplicates will deduplicate elements provided in multiple KV tuple
@@ -191,27 +157,25 @@ func pretty(value interface{}) string {
191157
return strings.TrimSpace(string(buffer.Bytes()))
192158
}
193159

194-
func (l klogger) Info(msg string, kvList ...interface{}) {
195-
if l.Enabled() {
196-
switch l.format {
197-
case FormatSerialize:
198-
msgStr := flatten("msg", msg)
199-
trimmed := trimDuplicates(l.values, kvList)
200-
fixedStr := flatten(trimmed[0]...)
201-
userStr := flatten(trimmed[1]...)
202-
klog.InfoDepth(framesToCaller()+l.callDepth, l.prefix, " ", msgStr, " ", fixedStr, " ", userStr)
203-
case FormatKlog:
204-
trimmed := trimDuplicates(l.values, kvList)
205-
if l.prefix != "" {
206-
msg = l.prefix + ": " + msg
207-
}
208-
klog.InfoSDepth(framesToCaller()+l.callDepth, msg, append(trimmed[0], trimmed[1]...)...)
160+
func (l klogger) Info(level int, msg string, kvList ...interface{}) {
161+
switch l.format {
162+
case FormatSerialize:
163+
msgStr := flatten("msg", msg)
164+
trimmed := trimDuplicates(l.values, kvList)
165+
fixedStr := flatten(trimmed[0]...)
166+
userStr := flatten(trimmed[1]...)
167+
klog.InfoDepth(l.callDepth+1, l.prefix, " ", msgStr, " ", fixedStr, " ", userStr)
168+
case FormatKlog:
169+
trimmed := trimDuplicates(l.values, kvList)
170+
if l.prefix != "" {
171+
msg = l.prefix + ": " + msg
209172
}
173+
klog.InfoSDepth(l.callDepth+1, msg, append(trimmed[0], trimmed[1]...)...)
210174
}
211175
}
212176

213-
func (l klogger) Enabled() bool {
214-
return bool(klog.V(klog.Level(l.level)).Enabled())
177+
func (l klogger) Enabled(level int) bool {
178+
return klog.V(klog.Level(level)).Enabled()
215179
}
216180

217181
func (l klogger) Error(err error, msg string, kvList ...interface{}) {
@@ -226,45 +190,38 @@ func (l klogger) Error(err error, msg string, kvList ...interface{}) {
226190
trimmed := trimDuplicates(l.values, kvList)
227191
fixedStr := flatten(trimmed[0]...)
228192
userStr := flatten(trimmed[1]...)
229-
klog.ErrorDepth(framesToCaller()+l.callDepth, l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr)
193+
klog.ErrorDepth(l.callDepth+1, l.prefix, " ", msgStr, " ", errStr, " ", fixedStr, " ", userStr)
230194
case FormatKlog:
231195
trimmed := trimDuplicates(l.values, kvList)
232196
if l.prefix != "" {
233197
msg = l.prefix + ": " + msg
234198
}
235-
klog.ErrorSDepth(framesToCaller()+l.callDepth, err, msg, append(trimmed[0], trimmed[1]...)...)
199+
klog.ErrorSDepth(l.callDepth+1, err, msg, append(trimmed[0], trimmed[1]...)...)
236200
}
237201
}
238202

239-
func (l klogger) V(level int) logr.Logger {
240-
new := l.clone()
241-
new.level = level
242-
return new
243-
}
244-
245203
// WithName returns a new logr.Logger with the specified name appended. klogr
246204
// uses '/' characters to separate name elements. Callers should not pass '/'
247205
// in the provided name string, but this library does not actually enforce that.
248-
func (l klogger) WithName(name string) logr.Logger {
249-
new := l.clone()
206+
func (l klogger) WithName(name string) logr.LogSink {
250207
if len(l.prefix) > 0 {
251-
new.prefix = l.prefix + "/"
208+
l.prefix = l.prefix + "/"
252209
}
253-
new.prefix += name
254-
return new
210+
l.prefix += name
211+
return &l
255212
}
256213

257-
func (l klogger) WithValues(kvList ...interface{}) logr.Logger {
258-
new := l.clone()
259-
new.values = append(new.values, kvList...)
260-
return new
214+
func (l klogger) WithValues(kvList ...interface{}) logr.LogSink {
215+
// Three slice args forces a copy.
216+
n := len(l.values)
217+
l.values = append(l.values[:n:n], kvList...)
218+
return &l
261219
}
262220

263-
func (l klogger) WithCallDepth(depth int) logr.Logger {
264-
new := l.clone()
265-
new.callDepth += depth
266-
return new
221+
func (l klogger) WithCallDepth(depth int) logr.LogSink {
222+
l.callDepth += depth
223+
return &l
267224
}
268225

269-
var _ logr.Logger = klogger{}
270-
var _ logr.CallDepthLogger = klogger{}
226+
var _ logr.LogSink = &klogger{}
227+
var _ logr.CallDepthLogSink = &klogger{}

‎klogr/klogr_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func testOutput(t *testing.T, format string) {
110110
`,
111111
},
112112
"should correctly handle odd-numbers of KVs": {
113+
klogr: new(),
113114
text: "test",
114115
keysAndValues: []interface{}{"akey", "avalue", "akey2"},
115116
expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"=null
@@ -118,6 +119,7 @@ func testOutput(t *testing.T, format string) {
118119
`,
119120
},
120121
"should correctly html characters": {
122+
klogr: new(),
121123
text: "test",
122124
keysAndValues: []interface{}{"akey", "<&>"},
123125
expectedOutput: ` "msg"="test" "akey"="<&>"
@@ -169,19 +171,15 @@ func testOutput(t *testing.T, format string) {
169171
}
170172
for n, test := range tests {
171173
t.Run(n, func(t *testing.T) {
172-
klogr := test.klogr
173-
if klogr == nil {
174-
klogr = new()
175-
}
176174

177175
// hijack the klog output
178176
tmpWriteBuffer := bytes.NewBuffer(nil)
179177
klog.SetOutput(tmpWriteBuffer)
180178

181179
if test.err != nil {
182-
klogr.Error(test.err, test.text, test.keysAndValues...)
180+
test.klogr.Error(test.err, test.text, test.keysAndValues...)
183181
} else {
184-
klogr.Info(test.text, test.keysAndValues...)
182+
test.klogr.Info(test.text, test.keysAndValues...)
185183
}
186184

187185
// call Flush to ensure the text isn't still buffered

0 commit comments

Comments
 (0)
Please sign in to comment.