Skip to content

Commit

Permalink
Disable log colors when not using a terminal (redux) (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Apr 12, 2024
1 parent 0d773ea commit e3293eb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 1 addition & 5 deletions integration_tests/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/postgres/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
6 changes: 5 additions & 1 deletion lib/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down

0 comments on commit e3293eb

Please sign in to comment.