Skip to content

Commit

Permalink
Merge pull request #378 from aimuz/fix-370
Browse files Browse the repository at this point in the history
fix: SetLogger via klog.SetLogger will output an unexpected newline
  • Loading branch information
k8s-ci-robot committed May 20, 2023
2 parents 6ded808 + 1e2789b commit 5ac9b76
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 5ac9b76

Please sign in to comment.