Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SetLogger via klog.SetLogger will output an unexpected newline #378

Merged
merged 1 commit into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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