Skip to content

Commit ac8ca7d

Browse files
authored
feat: respect NO_COLOR environment variable (#1911)
1 parent 8a212de commit ac8ca7d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

log/logger.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"maps"
9+
"os"
910
"strings"
1011
"sync"
1112
"sync/atomic"
@@ -100,13 +101,17 @@ func ConfigureLogger(logger *logrus.Logger, cfg *Config) {
100101

101102
switch cfg.Format {
102103
case FormatTypeText:
104+
// Respect NO_COLOR env var (https://no-color.org/)
105+
noColor := os.Getenv("NO_COLOR") != ""
106+
103107
logFormatter := &prefixed.TextFormatter{
104108
TimestampFormat: "2006-01-02 15:04:05",
105109
FullTimestamp: true,
106110
ForceFormatting: true,
107111
ForceColors: false,
108112
QuoteEmptyFields: true,
109113
DisableTimestamp: !cfg.Timestamp,
114+
DisableColors: noColor,
110115
}
111116

112117
logFormatter.SetColorScheme(&prefixed.ColorScheme{
@@ -116,8 +121,12 @@ func ConfigureLogger(logger *logrus.Logger, cfg *Config) {
116121

117122
logger.SetFormatter(logFormatter)
118123

119-
// Windows does not support ANSI colors
120-
logger.SetOutput(colorable.NewColorableStdout())
124+
if noColor {
125+
logger.SetOutput(os.Stdout)
126+
} else {
127+
// Windows does not support ANSI colors
128+
logger.SetOutput(colorable.NewColorableStdout())
129+
}
121130

122131
case FormatTypeJson:
123132
logger.SetFormatter(&logrus.JSONFormatter{})

0 commit comments

Comments
 (0)