From b264ba77c38df37dfa1eaa9fb31b2b898d4461e3 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Fri, 21 Jul 2017 16:06:23 +0200 Subject: [PATCH 1/2] add test in text formatter checking the formatting --- text_formatter_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/text_formatter_test.go b/text_formatter_test.go index ecb8f12..7f7b78c 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -9,6 +9,25 @@ import ( "time" ) +func TestFormatting(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + + testCases := []struct { + value string + expected string + }{ + {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo \n"}, + } + + for _, tc := range testCases { + b, _ := tf.Format(WithField("test", tc.value)) + + if string(b) != tc.expected { + t.Errorf("formatting expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected) + } + } +} + func TestQuoting(t *testing.T) { tf := &TextFormatter{DisableColors: true} From 159e991025068d28cccc4138c1c19ff93b1e1df3 Mon Sep 17 00:00:00 2001 From: dmathieu <42@dmathieu.com> Date: Fri, 21 Jul 2017 16:14:28 +0200 Subject: [PATCH 2/2] only add a space between text entries if there is already content So we don't have a trailing space at the end of each log line --- text_formatter.go | 5 +++-- text_formatter_test.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index cf3f17f..ec8d473 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -154,11 +154,12 @@ func (f *TextFormatter) needsQuoting(text string) bool { } func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) { - + if b.String() != "" { + b.WriteByte(' ') + } b.WriteString(key) b.WriteByte('=') f.appendValue(b, value) - b.WriteByte(' ') } func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { diff --git a/text_formatter_test.go b/text_formatter_test.go index 7f7b78c..d93b931 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -16,7 +16,7 @@ func TestFormatting(t *testing.T) { value string expected string }{ - {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo \n"}, + {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"}, } for _, tc := range testCases {