From e8dbf2c83c2a35ac8494e84c2a85e0fcad7916b4 Mon Sep 17 00:00:00 2001 From: Lasse Gaardsholt Date: Mon, 4 Sep 2023 16:03:39 +0200 Subject: [PATCH 1/3] added support for NO_COLOR Signed-off-by: Lasse Gaardsholt --- console.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/console.go b/console.go index 8b0e0c61..e66d8886 100644 --- a/console.go +++ b/console.go @@ -312,6 +312,11 @@ func needsQuote(s string) bool { // colorize returns the string s wrapped in ANSI code c, unless disabled is true. func colorize(s interface{}, c int, disabled bool) string { + e := os.Getenv("NO_COLOR") + if e == "true" { + disabled = true + } + if disabled { return fmt.Sprintf("%s", s) } From 6a31fc3ee2f1b4154b600af94c420efc00b4b773 Mon Sep 17 00:00:00 2001 From: Lasse Gaardsholt Date: Mon, 4 Sep 2023 16:19:43 +0200 Subject: [PATCH 2/3] added unit test for `NO_COLOR` Signed-off-by: Lasse Gaardsholt --- console_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/console_test.go b/console_test.go index 18c2db70..6460c8fe 100644 --- a/console_test.go +++ b/console_test.go @@ -104,6 +104,44 @@ func TestConsoleWriter(t *testing.T) { } }) + t.Run("NO_COLOR = true", func(t *testing.T) { + os.Setenv("NO_COLOR", "true") + + buf := &bytes.Buffer{} + w := zerolog.ConsoleWriter{Out: buf} + + _, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`)) + if err != nil { + t.Errorf("Unexpected error when writing output: %s", err) + } + + expectedOutput := " WRN Foobar\n" + actualOutput := buf.String() + if actualOutput != expectedOutput { + t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput) + } + os.Unsetenv("NO_COLOR") + }) + + t.Run("NO_COLOR = false", func(t *testing.T) { + os.Setenv("NO_COLOR", "false") + + buf := &bytes.Buffer{} + w := zerolog.ConsoleWriter{Out: buf} + + _, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`)) + if err != nil { + t.Errorf("Unexpected error when writing output: %s", err) + } + + 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) + } + os.Unsetenv("NO_COLOR") + }) + t.Run("Write fields", func(t *testing.T) { buf := &bytes.Buffer{} w := zerolog.ConsoleWriter{Out: buf, NoColor: true} From 04c188f833946a66fbd74cce5b15d6a97ca8fdbd Mon Sep 17 00:00:00 2001 From: Lasse Gaardsholt Date: Mon, 4 Sep 2023 21:27:40 +0200 Subject: [PATCH 3/3] NO_COLOR can now be set to anything Signed-off-by: Lasse Gaardsholt --- console.go | 2 +- console_test.go | 21 +-------------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/console.go b/console.go index e66d8886..28279885 100644 --- a/console.go +++ b/console.go @@ -313,7 +313,7 @@ func needsQuote(s string) bool { // colorize returns the string s wrapped in ANSI code c, unless disabled is true. func colorize(s interface{}, c int, disabled bool) string { e := os.Getenv("NO_COLOR") - if e == "true" { + if e != "" { disabled = true } diff --git a/console_test.go b/console_test.go index 6460c8fe..0265672d 100644 --- a/console_test.go +++ b/console_test.go @@ -105,7 +105,7 @@ func TestConsoleWriter(t *testing.T) { }) t.Run("NO_COLOR = true", func(t *testing.T) { - os.Setenv("NO_COLOR", "true") + os.Setenv("NO_COLOR", "anything") buf := &bytes.Buffer{} w := zerolog.ConsoleWriter{Out: buf} @@ -123,25 +123,6 @@ func TestConsoleWriter(t *testing.T) { os.Unsetenv("NO_COLOR") }) - t.Run("NO_COLOR = false", func(t *testing.T) { - os.Setenv("NO_COLOR", "false") - - buf := &bytes.Buffer{} - w := zerolog.ConsoleWriter{Out: buf} - - _, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`)) - if err != nil { - t.Errorf("Unexpected error when writing output: %s", err) - } - - 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) - } - os.Unsetenv("NO_COLOR") - }) - t.Run("Write fields", func(t *testing.T) { buf := &bytes.Buffer{} w := zerolog.ConsoleWriter{Out: buf, NoColor: true}