Skip to content

Commit

Permalink
Fix colors not being enabled in some forced contexts.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
evanphx committed Dec 4, 2023
1 parent 1ea3660 commit 4a8b5e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 4a8b5e1

Please sign in to comment.