Skip to content

Commit

Permalink
Remove extra space in console when there is no message (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar committed Feb 19, 2022
1 parent fc26014 commit 361cdf6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions console.go
Expand Up @@ -145,7 +145,8 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
}
sort.Strings(fields)

if len(fields) > 0 {
// Write space only if something has already been written to the buffer, and if there are fields.
if buf.Len() > 0 && len(fields) > 0 {
buf.WriteByte(' ')
}

Expand Down Expand Up @@ -268,10 +269,10 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
var s = f(evt[p])

if len(s) > 0 {
buf.WriteString(s)
if p != w.PartsOrder[len(w.PartsOrder)-1] { // Skip space for last part
buf.WriteByte(' ')
if buf.Len() > 0 {
buf.WriteByte(' ') // Write space only if not the first part
}
buf.WriteString(s)
}
}

Expand Down
2 changes: 1 addition & 1 deletion console_test.go
Expand Up @@ -196,7 +196,7 @@ func TestConsoleWriter(t *testing.T) {
t.Errorf("Unexpected error when writing output: %s", err)
}

expectedOutput := "<nil> DBG foo=bar\n"
expectedOutput := "<nil> DBG foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
Expand Down

0 comments on commit 361cdf6

Please sign in to comment.