Skip to content

Commit

Permalink
Disable log colors if not a terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Apr 11, 2024
1 parent 7f9ab89 commit 74f21f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/lib/pq v1.10.9
github.com/lmittmann/tint v1.0.4
github.com/mattn/go-isatty v0.0.20
github.com/microsoft/go-mssqldb v1.7.0
github.com/samber/slog-multi v1.0.2
github.com/samber/slog-sentry/v2 v2.4.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/microsoft/go-mssqldb v1.7.0 h1:sgMPW0HA6Ihd37Yx0MzHyKD726C2kY/8KJsQtXHNaAs=
Expand Down Expand Up @@ -645,6 +647,7 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
Expand Down
6 changes: 5 additions & 1 deletion lib/logger/log.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 @@ -21,7 +22,10 @@ func NewLogger(verbose bool, sentryCfg *config.Sentry) (*slog.Logger, func()) {
tintLogLevel = slog.LevelDebug
}

handler := tint.NewHandler(os.Stderr, &tint.Options{Level: tintLogLevel})
handler := tint.NewHandler(os.Stderr, &tint.Options{
Level: tintLogLevel,
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
})
if sentryCfg != nil && sentryCfg.DSN != "" {
if err := sentry.Init(sentry.ClientOptions{Dsn: sentryCfg.DSN}); err != nil {
slog.New(handler).Warn("Failed to enable Sentry output", slog.Any("err", err))
Expand Down

0 comments on commit 74f21f1

Please sign in to comment.