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.
This commit is contained in:
Kevin McConnell 2019-02-07 15:45:02 +00:00 committed by Olivier Poitrey
parent aa55558e4c
commit 4daee2b758
2 changed files with 23 additions and 9 deletions

View File

@ -13,11 +13,6 @@ import (
"time" "time"
) )
const (
colorBold = iota + 1
colorFaint
)
const ( const (
colorBlack = iota + 30 colorBlack = iota + 30
colorRed colorRed
@ -27,6 +22,9 @@ const (
colorMagenta colorMagenta
colorCyan colorCyan
colorWhite colorWhite
colorBold = 1
colorDarkGray = 90
) )
var ( var (
@ -299,7 +297,7 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
case json.Number: case json.Number:
t = tt.String() 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, cwd)
c = strings.TrimPrefix(c, "/") c = strings.TrimPrefix(c, "/")
} }
c = colorize(c, colorBold, noColor) + colorize(" >", colorFaint, noColor) c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor)
} }
return c return c
} }
@ -354,7 +352,7 @@ func consoleDefaultFormatMessage(i interface{}) string {
func consoleDefaultFormatFieldName(noColor bool) Formatter { func consoleDefaultFormatFieldName(noColor bool) Formatter {
return func(i interface{}) string { return func(i interface{}) string {
return colorize(fmt.Sprintf("%s=", i), colorFaint, noColor) return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
} }
} }

View File

@ -97,7 +97,7 @@ func TestConsoleWriter(t *testing.T) {
t.Errorf("Unexpected error when writing output: %s", err) t.Errorf("Unexpected error when writing output: %s", err)
} }
expectedOutput := "\x1b[2m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n" expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
actualOutput := buf.String() actualOutput := buf.String()
if actualOutput != expectedOutput { if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", 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<nil>\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) { t.Run("Write error field", func(t *testing.T) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true} w := zerolog.ConsoleWriter{Out: buf, NoColor: true}