From 1e2789b9af1994a23484e142a861576db1634079 Mon Sep 17 00:00:00 2001 From: aimuz Date: Thu, 11 May 2023 22:43:54 +0800 Subject: [PATCH] fix: SetLogger via klog.SetLogger will output an unexpected newline 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 https://github.com/kubernetes/klog/issues/370 Signed-off-by: aimuz --- examples/flushing/.gitignore | 1 + klog.go | 3 +++ klog_test.go | 4 ++-- test/zapr.go | 12 ++++++------ 4 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 examples/flushing/.gitignore diff --git a/examples/flushing/.gitignore b/examples/flushing/.gitignore new file mode 100644 index 000000000..2383fed92 --- /dev/null +++ b/examples/flushing/.gitignore @@ -0,0 +1 @@ +myfile.log diff --git a/klog.go b/klog.go index 152f8a6bd..a4290d11d 100644 --- a/klog.go +++ b/klog.go @@ -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 { diff --git a/klog_test.go b/klog_test.go index 76303e068..7b6ebdb6f 100644 --- a/klog_test.go +++ b/klog_test.go @@ -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: "", }, }} diff --git a/test/zapr.go b/test/zapr.go index 7b1594429..6c7e809fb 100644 --- a/test/zapr.go +++ b/test/zapr.go @@ -171,22 +171,22 @@ I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" // klog.Info `I output.go:] "helloworld\n" -`: `{"caller":"test/output.go:","msg":"helloworld\n","v":0} +`: `{"caller":"test/output.go:","msg":"helloworld","v":0} `, // klog.Infoln `I output.go:] "hello world\n" -`: `{"caller":"test/output.go:","msg":"hello world\n","v":0} +`: `{"caller":"test/output.go:","msg":"hello world","v":0} `, // klog.Error `E output.go:] "helloworld\n" -`: `{"caller":"test/output.go:","msg":"helloworld\n"} +`: `{"caller":"test/output.go:","msg":"helloworld"} `, // klog.Errorln `E output.go:] "hello world\n" -`: `{"caller":"test/output.go:","msg":"hello world\n"} +`: `{"caller":"test/output.go:","msg":"hello world"} `, // klog.ErrorS @@ -201,12 +201,12 @@ I output.go:] "odd WithValues" keyWithoutValue="(MISSING)" // klog.V(1).Info `I output.go:] "hellooneworld\n" -`: `{"caller":"test/output.go:","msg":"hellooneworld\n","v":1} +`: `{"caller":"test/output.go:","msg":"hellooneworld","v":1} `, // klog.V(1).Infoln `I output.go:] "hello one world\n" -`: `{"caller":"test/output.go:","msg":"hello one world\n","v":1} +`: `{"caller":"test/output.go:","msg":"hello one world","v":1} `, // klog.V(1).ErrorS