From e3293ebc5f0c75bb0c06f14c229d61159fa95b74 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:18:52 -0700 Subject: [PATCH] Disable log colors when not using a terminal (redux) (#355) --- integration_tests/mysql/main.go | 6 +----- integration_tests/postgres/main.go | 2 +- lib/logger/logger.go | 6 +++++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/integration_tests/mysql/main.go b/integration_tests/mysql/main.go index 55c40f14..c7eb44f6 100644 --- a/integration_tests/mysql/main.go +++ b/integration_tests/mysql/main.go @@ -10,7 +10,6 @@ import ( "os" "github.com/lmittmann/tint" - "github.com/mattn/go-isatty" "github.com/artie-labs/reader/config" "github.com/artie-labs/reader/integration_tests/utils" @@ -24,10 +23,7 @@ func main() { if err := os.Setenv("TZ", "UTC"); err != nil { logger.Fatal("Unable to set TZ env var: %w", err) } - slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{ - Level: slog.LevelInfo, - NoColor: !isatty.IsTerminal(os.Stderr.Fd()), - }))) + slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{}))) var mysqlHost string = os.Getenv("MYSQL_HOST") if mysqlHost == "" { diff --git a/integration_tests/postgres/main.go b/integration_tests/postgres/main.go index f51aa793..b745f097 100644 --- a/integration_tests/postgres/main.go +++ b/integration_tests/postgres/main.go @@ -24,7 +24,7 @@ func main() { if err := os.Setenv("TZ", "UTC"); err != nil { logger.Fatal("Unable to set TZ env var: %w", err) } - slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelInfo}))) + slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{}))) var pgHost string = os.Getenv("PG_HOST") if pgHost == "" { diff --git a/lib/logger/logger.go b/lib/logger/logger.go index dfac3df8..c562345d 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -7,6 +7,7 @@ import ( "github.com/getsentry/sentry-go" "github.com/lmittmann/tint" + "github.com/mattn/go-isatty" slogmulti "github.com/samber/slog-multi" slogsentry "github.com/samber/slog-sentry/v2" @@ -16,7 +17,10 @@ import ( var handlersToTerminate []func() func NewLogger(settings *config.Settings) (*slog.Logger, func()) { - handler := tint.NewHandler(os.Stderr, &tint.Options{Level: slog.LevelInfo}) + handler := tint.NewHandler(os.Stderr, &tint.Options{ + Level: slog.LevelInfo, + NoColor: !isatty.IsTerminal(os.Stderr.Fd()), + }) if settings != nil && settings.Reporting != nil && settings.Reporting.Sentry != nil && settings.Reporting.Sentry.DSN != "" { if err := sentry.Init(sentry.ClientOptions{Dsn: settings.Reporting.Sentry.DSN}); err != nil {