From 4daee2b758981d0c8c6602a0d0334db67638b76f Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Thu, 7 Feb 2019 15:45:02 +0000 Subject: [PATCH] Don't use faint text in colorized console output (#131) The faint text style doesn't seem to be supported by all terminals, which means the default console output loses most of its coloring on them. This commit changes the color scheme to more widely-supported colors. This also matches how it looked in earlier versions of this library. --- console.go | 14 ++++++-------- console_test.go | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/console.go b/console.go index e13d419..90829b0 100644 --- a/console.go +++ b/console.go @@ -13,11 +13,6 @@ import ( "time" ) -const ( - colorBold = iota + 1 - colorFaint -) - const ( colorBlack = iota + 30 colorRed @@ -27,6 +22,9 @@ const ( colorMagenta colorCyan colorWhite + + colorBold = 1 + colorDarkGray = 90 ) var ( @@ -299,7 +297,7 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter { case json.Number: t = tt.String() } - return colorize(t, colorFaint, noColor) + return colorize(t, colorDarkGray, noColor) } } @@ -342,7 +340,7 @@ func consoleDefaultFormatCaller(noColor bool) Formatter { c = strings.TrimPrefix(c, cwd) c = strings.TrimPrefix(c, "/") } - c = colorize(c, colorBold, noColor) + colorize(" >", colorFaint, noColor) + c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor) } return c } @@ -354,7 +352,7 @@ func consoleDefaultFormatMessage(i interface{}) string { func consoleDefaultFormatFieldName(noColor bool) Formatter { return func(i interface{}) string { - return colorize(fmt.Sprintf("%s=", i), colorFaint, noColor) + return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor) } } diff --git a/console_test.go b/console_test.go index 5bdfdaa..2ff2a43 100644 --- a/console_test.go +++ b/console_test.go @@ -97,7 +97,7 @@ func TestConsoleWriter(t *testing.T) { t.Errorf("Unexpected error when writing output: %s", err) } - expectedOutput := "\x1b[2m\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n" + expectedOutput := "\x1b[90m\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n" actualOutput := buf.String() if actualOutput != expectedOutput { t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput) @@ -121,6 +121,22 @@ func TestConsoleWriter(t *testing.T) { } }) + t.Run("Write colorized fields", func(t *testing.T) { + buf := &bytes.Buffer{} + w := zerolog.ConsoleWriter{Out: buf, NoColor: false} + + _, err := w.Write([]byte(`{"level" : "warn", "message" : "Foobar", "foo": "bar"}`)) + if err != nil { + t.Errorf("Unexpected error when writing output: %s", err) + } + + expectedOutput := "\x1b[90m\x1b[0m \x1b[31mWRN\x1b[0m Foobar \x1b[36mfoo=\x1b[0mbar\n" + actualOutput := buf.String() + if actualOutput != expectedOutput { + t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput) + } + }) + t.Run("Write error field", func(t *testing.T) { buf := &bytes.Buffer{} w := zerolog.ConsoleWriter{Out: buf, NoColor: true}