Skip to content

Commit

Permalink
Don't use faint text in colorized console output (rs#131)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kevinmcconnell authored and rs committed Feb 7, 2019
1 parent aa55558 commit 4daee2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
14 changes: 6 additions & 8 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"time"
)

const (
colorBold = iota + 1
colorFaint
)

const (
colorBlack = iota + 30
colorRed
Expand All @@ -27,6 +22,9 @@ const (
colorMagenta
colorCyan
colorWhite

colorBold = 1
colorDarkGray = 90
)

var (
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
}

Expand Down
18 changes: 17 additions & 1 deletion console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestConsoleWriter(t *testing.T) {
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()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
Expand All @@ -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) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
Expand Down

0 comments on commit 4daee2b

Please sign in to comment.