Skip to content

Commit

Permalink
Start with a colorful log if stderr is TTY, but also respect NO_COLOR
Browse files Browse the repository at this point in the history
This will ensure we have the same behavior as previous k6 version there's an error before setupLoggers() is executed, e.g. when parsing a wrong CLI flag. However, we will now also respect NO_COLOR and K6_NO_COLOR and disable it when they are specified.
  • Loading branch information
na-- committed Mar 7, 2022
1 parent 1e8979f commit 6774d60
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ func newGlobalState(ctx context.Context) *globalState {
stdOut := &consoleWriter{os.Stdout, colorable.NewColorable(os.Stdout), stdoutTTY, outMutex, nil}
stdErr := &consoleWriter{os.Stderr, colorable.NewColorable(os.Stderr), stderrTTY, outMutex, nil}

logger := getDefaultLogger(stdErr)
logger := &logrus.Logger{
Out: stdErr,
Formatter: &logrus.TextFormatter{
ForceColors: stderrTTY,
// This is a hack just for this env var, since we need the logger
// before we can parse all of the env vars...
DisableColors: !stderrTTY || os.Getenv("NO_COLOR") != "" || os.Getenv("K6_NO_COLOR") != "",
},
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}

confDir, err := os.UserConfigDir()
if err != nil {
Expand All @@ -110,30 +120,26 @@ func newGlobalState(ctx context.Context) *globalState {
defaultFlags := getDefaultFlags(confDir)

return &globalState{
ctx: ctx,
fs: afero.NewOsFs(),
getwd: os.Getwd,
args: append(make([]string, 0, len(os.Args)), os.Args...), // copy
envVars: envVars,
defaultFlags: defaultFlags,
flags: getFlags(defaultFlags, envVars),
outMutex: outMutex,
stdOut: stdOut,
stdErr: stdErr,
stdIn: os.Stdin,
signalNotify: signal.Notify,
signalStop: signal.Stop,
logger: logger,
fallbackLogger: getDefaultLogger(stdErr), // we may modify the other one
}
}

func getDefaultLogger(out io.Writer) *logrus.Logger {
return &logrus.Logger{
Out: out,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
ctx: ctx,
fs: afero.NewOsFs(),
getwd: os.Getwd,
args: append(make([]string, 0, len(os.Args)), os.Args...), // copy
envVars: envVars,
defaultFlags: defaultFlags,
flags: getFlags(defaultFlags, envVars),
outMutex: outMutex,
stdOut: stdOut,
stdErr: stdErr,
stdIn: os.Stdin,
signalNotify: signal.Notify,
signalStop: signal.Stop,
logger: logger,
fallbackLogger: &logrus.Logger{ // we may modify the other one
Out: stdErr,
Formatter: new(logrus.TextFormatter), // no fancy formatting here
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
},
}
}

Expand Down Expand Up @@ -163,6 +169,12 @@ func getFlags(defaultFlags globalFlags, env map[string]string) globalFlags {
if val, ok := env["K6_LOG_FORMAT"]; ok {
result.logFormat = val
}
if _, ok := env["K6_NO_COLOR"]; ok {
result.noColor = true
}
if _, ok := env["NO_COLOR"]; ok { // https://no-color.org/
result.noColor = true
}
return result
}

Expand Down

0 comments on commit 6774d60

Please sign in to comment.