Skip to content

Commit

Permalink
Merge pull request #283 from jklaw90/fix-empty
Browse files Browse the repository at this point in the history
Panic on empty info with custom logr
  • Loading branch information
k8s-ci-robot committed Jan 19, 2022
2 parents 4ce6ac6 + 929e7a9 commit 872d9d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion klog.go
Expand Up @@ -729,7 +729,7 @@ func (l *loggingT) printDepth(s severity, logger *logr.Logger, filter LogFilter,
args = filter.Filter(args)
}
fmt.Fprint(buf, args...)
if buf.Bytes()[buf.Len()-1] != '\n' {
if buf.Len() == 0 || buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
l.output(s, logger, buf, depth, file, line, false)
Expand Down
36 changes: 36 additions & 0 deletions klog_test.go
Expand Up @@ -1416,6 +1416,42 @@ func TestLogFilter(t *testing.T) {
}
}

func TestInfoWithLogr(t *testing.T) {
logger := new(testLogr)

testDataInfo := []struct {
msg string
expected testLogrEntry
}{{
msg: "foo",
expected: testLogrEntry{
severity: infoLog,
msg: "foo\n",
},
}, {
msg: "",
expected: testLogrEntry{
severity: infoLog,
msg: "\n",
},
}}

for _, data := range testDataInfo {
t.Run(data.msg, func(t *testing.T) {
l := logr.New(logger)
SetLogger(l)
defer ClearLogger()
defer logger.reset()

Info(data.msg)

if !reflect.DeepEqual(logger.entries, []testLogrEntry{data.expected}) {
t.Errorf("expected: %+v; but got: %+v", []testLogrEntry{data.expected}, logger.entries)
}
})
}
}

func TestInfoSWithLogr(t *testing.T) {
logger := new(testLogr)

Expand Down

0 comments on commit 872d9d9

Please sign in to comment.