Skip to content

Commit

Permalink
fix: SetLogger via klog.SetLogger will output an unexpected newline
Browse files Browse the repository at this point in the history
klog always adds a newline to the msg. klog will work fine without klog.

Set logr with klog.SetLogger, and klog will also pass the msg with the newline added to logr, which will result in an accidental newline being added.

step1: klog.Info("hello world")
step2: msg = msg + "\n"
step3: stdout.W(msg) or logr.Info(msg[:len(msg)-1])

fix #370

Signed-off-by: aimuz <mr.imuz@gmail.com>
  • Loading branch information
aimuz committed May 17, 2023
1 parent d731661 commit 1e2789b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/flushing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myfile.log
3 changes: 3 additions & 0 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,9 @@ func (l *loggingT) output(s severity.Severity, logger *logWriter, buf *buffer.Bu
if logger.writeKlogBuffer != nil {
logger.writeKlogBuffer(data)
} else {
if len(data) > 0 && data[len(data)-1] == '\n' {
data = data[:len(data)-1]
}
// TODO: set 'severity' and caller information as structured log info
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
if s == severity.ErrorLog {
Expand Down
4 changes: 2 additions & 2 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,13 +1484,13 @@ func TestInfoWithLogr(t *testing.T) {
msg: "foo",
expected: testLogrEntry{
severity: severity.InfoLog,
msg: "foo\n",
msg: "foo",
},
}, {
msg: "",
expected: testLogrEntry{
severity: severity.InfoLog,
msg: "\n",
msg: "",
},
}}

Expand Down
12 changes: 6 additions & 6 deletions test/zapr.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,22 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"

// klog.Info
`I output.go:<LINE>] "helloworld\n"
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld\n","v":0}
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld","v":0}
`,

// klog.Infoln
`I output.go:<LINE>] "hello world\n"
`: `{"caller":"test/output.go:<LINE>","msg":"hello world\n","v":0}
`: `{"caller":"test/output.go:<LINE>","msg":"hello world","v":0}
`,

// klog.Error
`E output.go:<LINE>] "helloworld\n"
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld\n"}
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld"}
`,

// klog.Errorln
`E output.go:<LINE>] "hello world\n"
`: `{"caller":"test/output.go:<LINE>","msg":"hello world\n"}
`: `{"caller":"test/output.go:<LINE>","msg":"hello world"}
`,

// klog.ErrorS
Expand All @@ -201,12 +201,12 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"

// klog.V(1).Info
`I output.go:<LINE>] "hellooneworld\n"
`: `{"caller":"test/output.go:<LINE>","msg":"hellooneworld\n","v":1}
`: `{"caller":"test/output.go:<LINE>","msg":"hellooneworld","v":1}
`,

// klog.V(1).Infoln
`I output.go:<LINE>] "hello one world\n"
`: `{"caller":"test/output.go:<LINE>","msg":"hello one world\n","v":1}
`: `{"caller":"test/output.go:<LINE>","msg":"hello one world","v":1}
`,

// klog.V(1).ErrorS
Expand Down

0 comments on commit 1e2789b

Please sign in to comment.