From 4a8b5e1cb0e998410e577d25de7162dbf6a73686 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Mon, 4 Dec 2023 10:54:35 -0800 Subject: [PATCH] Fix colors not being enabled in some forced contexts. Turns out that the color package we use was doing it's own "should colors be on or not" detection, which interferred with our own detection. This caused situations where colors were forced on to still be off (and, maddeningly, would show up in weird contexts because it dependent on Stdout being not a terminal to break when forced). --- intlogger.go | 20 +++++++++++++++++--- logger_test.go | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/intlogger.go b/intlogger.go index 0f63466..1a80008 100644 --- a/intlogger.go +++ b/intlogger.go @@ -55,11 +55,25 @@ var ( faintBoldColor = color.New(color.Faint, color.Bold) faintColor = color.New(color.Faint) - faintMultiLinePrefix = faintColor.Sprint(" | ") - faintFieldSeparator = faintColor.Sprint("=") - faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n") + faintMultiLinePrefix string + faintFieldSeparator string + faintFieldSeparatorWithNewLine string ) +func init() { + // Force all the colors to enabled because we do our own detection of color usage. + for _, c := range _levelToColor { + c.EnableColor() + } + + faintBoldColor.EnableColor() + faintColor.EnableColor() + + faintMultiLinePrefix = faintColor.Sprint(" | ") + faintFieldSeparator = faintColor.Sprint("=") + faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n") +} + // Make sure that intLogger is a Logger var _ Logger = &intLogger{} diff --git a/logger_test.go b/logger_test.go index 3892bb0..6a4acca 100644 --- a/logger_test.go +++ b/logger_test.go @@ -257,7 +257,7 @@ func TestLogger(t *testing.T) { assert.Equal(t, "[INFO] sublogger: this is test\n", rest) }) - t.Run("colorize", func(t *testing.T) { + t.Run("can force colors to on in any context", func(t *testing.T) { var buf bytes.Buffer logger := New(&LoggerOptions{