From cb34fe5305af0886d71f3b2b1de3be8d4102c5cf Mon Sep 17 00:00:00 2001 From: Mitar Date: Sat, 19 Feb 2022 16:22:20 +0100 Subject: [PATCH] Remove extra space in console when there is no message --- console.go | 9 +++++---- console_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/console.go b/console.go index e9ee5a6d..a3fb542c 100644 --- a/console.go +++ b/console.go @@ -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(' ') } @@ -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) } } diff --git a/console_test.go b/console_test.go index 508a544f..2b9ccb76 100644 --- a/console_test.go +++ b/console_test.go @@ -196,7 +196,7 @@ func TestConsoleWriter(t *testing.T) { t.Errorf("Unexpected error when writing output: %s", err) } - expectedOutput := " DBG foo=bar\n" + expectedOutput := " DBG foo=bar\n" actualOutput := buf.String() if actualOutput != expectedOutput { t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)