Skip to content

Commit 4171f3c

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

7 files changed

Lines changed: 117 additions & 140 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 37 additions & 29 deletions
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

Lines changed: 33 additions & 21 deletions
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)