Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: FTL_LOG_COLOR env to override tty detection #1508

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/log/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@ func ContextWithLogger(ctx context.Context, logger *Logger) context.Context {
}

func ContextWithNewDefaultLogger(ctx context.Context) context.Context {
return ContextWithLogger(ctx, Configure(os.Stderr, Config{Level: Debug}))
// Matches LOG_COLOR in log.Config. This is a special case for the default logger in testing.
color := os.Getenv("LOG_COLOR") != ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return ContextWithLogger(ctx, Configure(os.Stderr, Config{Level: Debug, Color: color}))
}
3 changes: 2 additions & 1 deletion internal/log/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Config struct {
Level Level `help:"Log level." default:"info" env:"LOG_LEVEL"`
JSON bool `help:"Log in JSON format." env:"LOG_JSON"`
Timestamps bool `help:"Include timestamps in text logs." env:"LOG_TIMESTAMPS"`
Color bool `help:"Enable colored output regardless of TTY." env:"LOG_COLOR"`
}

// Configure returns a new logger based on the config.
Expand All @@ -17,7 +18,7 @@ func Configure(w io.Writer, cfg Config) *Logger {
if cfg.JSON {
sink = newJSONSink(w)
} else {
sink = newPlainSink(w, cfg.Timestamps)
sink = newPlainSink(w, cfg.Timestamps, cfg.Color)
}
return New(cfg.Level, sink)
}
6 changes: 4 additions & 2 deletions internal/log/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ var colours = map[Level]string{

var _ Sink = (*plainSink)(nil)

func newPlainSink(w io.Writer, logTime bool) *plainSink {
func newPlainSink(w io.Writer, logTime bool, alwaysColor bool) *plainSink {
var isaTTY bool
if f, ok := w.(*os.File); ok {
if alwaysColor {
isaTTY = true
} else if f, ok := w.(*os.File); ok {
isaTTY = isatty.IsTerminal(f.Fd())
}
return &plainSink{
Expand Down
Loading