Skip to content

Commit 1e2789b

Browse files
committed
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 #370 Signed-off-by: aimuz <[email protected]>
1 parent d731661 commit 1e2789b

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

examples/flushing/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
myfile.log

klog.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,9 @@ func (l *loggingT) output(s severity.Severity, logger *logWriter, buf *buffer.Bu
873873
if logger.writeKlogBuffer != nil {
874874
logger.writeKlogBuffer(data)
875875
} else {
876+
if len(data) > 0 && data[len(data)-1] == '\n' {
877+
data = data[:len(data)-1]
878+
}
876879
// TODO: set 'severity' and caller information as structured log info
877880
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
878881
if s == severity.ErrorLog {

klog_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,13 +1484,13 @@ func TestInfoWithLogr(t *testing.T) {
14841484
msg: "foo",
14851485
expected: testLogrEntry{
14861486
severity: severity.InfoLog,
1487-
msg: "foo\n",
1487+
msg: "foo",
14881488
},
14891489
}, {
14901490
msg: "",
14911491
expected: testLogrEntry{
14921492
severity: severity.InfoLog,
1493-
msg: "\n",
1493+
msg: "",
14941494
},
14951495
}}
14961496

test/zapr.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,22 @@ I output.go:<LINE>] "odd WithValues" keyWithoutValue="(MISSING)"
171171

172172
// klog.Info
173173
`I output.go:<LINE>] "helloworld\n"
174-
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld\n","v":0}
174+
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld","v":0}
175175
`,
176176

177177
// klog.Infoln
178178
`I output.go:<LINE>] "hello world\n"
179-
`: `{"caller":"test/output.go:<LINE>","msg":"hello world\n","v":0}
179+
`: `{"caller":"test/output.go:<LINE>","msg":"hello world","v":0}
180180
`,
181181

182182
// klog.Error
183183
`E output.go:<LINE>] "helloworld\n"
184-
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld\n"}
184+
`: `{"caller":"test/output.go:<LINE>","msg":"helloworld"}
185185
`,
186186

187187
// klog.Errorln
188188
`E output.go:<LINE>] "hello world\n"
189-
`: `{"caller":"test/output.go:<LINE>","msg":"hello world\n"}
189+
`: `{"caller":"test/output.go:<LINE>","msg":"hello world"}
190190
`,
191191

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

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

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

212212
// klog.V(1).ErrorS

0 commit comments

Comments
 (0)