From 03377c6168987e4341f3ce44b640f31a0cd19af8 Mon Sep 17 00:00:00 2001 From: Derek Che Date: Sat, 3 Jan 2015 23:56:39 -0800 Subject: [PATCH 1/3] rename f.appendKeyValue to printKeyValue printKeyValue is working similar like printColored, not using any fields of TextFormatter, should be a util func instead of a method of TextFormatter. Signed-off-by: Derek Che --- text_formatter.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 78e7889..bbf6110 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -59,12 +59,12 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { printColored(b, entry, keys) } else { if !f.DisableTimestamp { - f.appendKeyValue(b, "time", entry.Time.Format(time.RFC3339)) + printKeyValue(b, "time", entry.Time.Format(time.RFC3339)) } - f.appendKeyValue(b, "level", entry.Level.String()) - f.appendKeyValue(b, "msg", entry.Message) + printKeyValue(b, "level", entry.Level.String()) + printKeyValue(b, "msg", entry.Message) for _, key := range keys { - f.appendKeyValue(b, key, entry.Data[key]) + printKeyValue(b, key, entry.Data[key]) } } @@ -104,7 +104,7 @@ func needsQuoting(text string) bool { return true } -func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key, value interface{}) { +func printKeyValue(b *bytes.Buffer, key, value interface{}) { switch value.(type) { case string: if needsQuoting(value.(string)) { From a243bbaa0b2e5314bf8e53831d9e0c1afa67de51 Mon Sep 17 00:00:00 2001 From: Derek Che Date: Sun, 4 Jan 2015 00:01:49 -0800 Subject: [PATCH 2/3] share common calling path in printKeyValue Signed-off-by: Derek Che --- text_formatter.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index bbf6110..ce19ead 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -107,18 +107,16 @@ func needsQuoting(text string) bool { func printKeyValue(b *bytes.Buffer, key, value interface{}) { switch value.(type) { case string: - if needsQuoting(value.(string)) { - fmt.Fprintf(b, "%v=%s ", key, value) - } else { - fmt.Fprintf(b, "%v=%q ", key, value) - } + break case error: - if needsQuoting(value.(error).Error()) { - fmt.Fprintf(b, "%v=%s ", key, value) - } else { - fmt.Fprintf(b, "%v=%q ", key, value) - } + value = value.(error).Error() default: fmt.Fprintf(b, "%v=%v ", key, value) } + + if needsQuoting(value.(string)) { + fmt.Fprintf(b, "%v=%s ", key, value) + } else { + fmt.Fprintf(b, "%v=%q ", key, value) + } } From dcbe8d66af81426652973ccf8788ae0b008dda08 Mon Sep 17 00:00:00 2001 From: Derek Che Date: Sun, 4 Jan 2015 00:46:15 +0000 Subject: [PATCH 3/3] make sure no leading or trailing spaces This changed printColored and printKeyValue to print in same way with prefix space instead of trailing space, to make it easier to slice out when returning in Format; The test cases are to make sure msg formartting doesn't include leading or trailing spaces; Closes #99 Signed-off-by: Derek Che --- text_formatter.go | 10 +++++----- text_formatter_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index ce19ead..c5cf225 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -69,7 +69,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { } b.WriteByte('\n') - return b.Bytes(), nil + return b.Bytes()[1:], nil } func printColored(b *bytes.Buffer, entry *Entry, keys []string) { @@ -85,7 +85,7 @@ func printColored(b *bytes.Buffer, entry *Entry, keys []string) { levelText := strings.ToUpper(entry.Level.String())[0:4] - fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message) + fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m[%04d] %-44s", levelColor, levelText, miniTS(), entry.Message) for _, k := range keys { v := entry.Data[k] fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%v", levelColor, k, v) @@ -111,12 +111,12 @@ func printKeyValue(b *bytes.Buffer, key, value interface{}) { case error: value = value.(error).Error() default: - fmt.Fprintf(b, "%v=%v ", key, value) + fmt.Fprintf(b, " %v=%v", key, value) } if needsQuoting(value.(string)) { - fmt.Fprintf(b, "%v=%s ", key, value) + fmt.Fprintf(b, " %v=%s", key, value) } else { - fmt.Fprintf(b, "%v=%q ", key, value) + fmt.Fprintf(b, " %v=%q", key, value) } } diff --git a/text_formatter_test.go b/text_formatter_test.go index f604f1b..8b71afb 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -31,3 +31,30 @@ func TestQuoting(t *testing.T) { checkQuoting(false, errors.New("invalid")) checkQuoting(true, errors.New("invalid argument")) } + +func TestTextPrint(t *testing.T) { + tf := &TextFormatter{DisableColors: true} + byts, _ := tf.Format(&Entry{Message: "msg content"}) + + // make sure no leading or trailing spaces + if string(byts) != + "time=\"0001-01-01T00:00:00Z\" level=panic msg=\"msg content\"\n" { + t.Errorf("not expected: %q", string(byts)) + } +} + +func TestColorPrint(t *testing.T) { + tf := &TextFormatter{ForceColors: true} + entry := WithField("testkey", "value") + entry.Message = "msg content" + byts, _ := tf.Format(entry) + + // make sure no leading or trailing spaces + if string(byts) != + "\x1b[31mPANI\x1b[0m[0000] " + + // length 44 plus one space + "msg content " + + "\x1b[31mtestkey\x1b[0m=value\n" { + t.Errorf("not expected: %q", string(byts)) + } +}