Skip to content

Commit 4a8b5e1

Browse files
committed
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).
1 parent 1ea3660 commit 4a8b5e1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

intlogger.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,25 @@ var (
5555

5656
faintBoldColor = color.New(color.Faint, color.Bold)
5757
faintColor = color.New(color.Faint)
58-
faintMultiLinePrefix = faintColor.Sprint(" | ")
59-
faintFieldSeparator = faintColor.Sprint("=")
60-
faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n")
58+
faintMultiLinePrefix string
59+
faintFieldSeparator string
60+
faintFieldSeparatorWithNewLine string
6161
)
6262

63+
func init() {
64+
// Force all the colors to enabled because we do our own detection of color usage.
65+
for _, c := range _levelToColor {
66+
c.EnableColor()
67+
}
68+
69+
faintBoldColor.EnableColor()
70+
faintColor.EnableColor()
71+
72+
faintMultiLinePrefix = faintColor.Sprint(" | ")
73+
faintFieldSeparator = faintColor.Sprint("=")
74+
faintFieldSeparatorWithNewLine = faintColor.Sprint("=\n")
75+
}
76+
6377
// Make sure that intLogger is a Logger
6478
var _ Logger = &intLogger{}
6579

logger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestLogger(t *testing.T) {
257257
assert.Equal(t, "[INFO] sublogger: this is test\n", rest)
258258
})
259259

260-
t.Run("colorize", func(t *testing.T) {
260+
t.Run("can force colors to on in any context", func(t *testing.T) {
261261
var buf bytes.Buffer
262262

263263
logger := New(&LoggerOptions{

0 commit comments

Comments
 (0)