Skip to content

Commit

Permalink
fix(logging): ⚡ improve logging setup
Browse files Browse the repository at this point in the history
- set color in logging based on output capabilities
- clean up logging handler generation
  • Loading branch information
joshuar committed Sep 2, 2024
1 parent 45f5189 commit a3e05bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ require (
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lmittmann/tint v1.0.5
github.com/mandykoh/prism v0.35.3
github.com/mattn/go-isatty v0.0.20
github.com/miekg/dns v1.1.41 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ github.com/matryer/moq v0.3.4 h1:czCFIos9rI2tyOehN9ktc/6bQ76N9J4xQ2n3dk063ac=
github.com/matryer/moq v0.3.4/go.mod h1:wqm9QObyoMuUtH81zFfs3EK6mXEcByy+TjvSROOXJ2U=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
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/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY=
Expand Down
53 changes: 29 additions & 24 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"

"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
slogmulti "github.com/samber/slog-multi"
)

Expand All @@ -30,55 +31,59 @@ var LevelNames = map[slog.Leveler]string{

//revive:disable:flag-parameter
func New(level string, logFile string) *slog.Logger {
// Create an slog consoleOpts object.
consoleOpts := &tint.Options{
ReplaceAttr: levelReplacer,
}
fileOpts := &tint.Options{
ReplaceAttr: levelReplacer,
NoColor: true,
}
var (
logLevel slog.Level
handler slog.Handler
)

// Set the log level.
switch level {
case "trace":
consoleOpts.Level = LevelTrace
consoleOpts.AddSource = true
fileOpts.Level = LevelTrace
fileOpts.AddSource = true
logLevel = LevelTrace
case "debug":
consoleOpts.Level = slog.LevelDebug
fileOpts.Level = slog.LevelDebug
logLevel = slog.LevelDebug
default:
consoleOpts.Level = slog.LevelInfo
fileOpts.Level = slog.LevelInfo
logLevel = slog.LevelInfo
}

// Set the slog handler
logHandler := slogmulti.Fanout(
tint.NewHandler(os.Stdout, consoleOpts),
)

// Unless no log file was requested, set up file logging.
if logFile != "" {
logFH, err := openLogFile(logFile)
if err != nil {
slog.Warn("unable to open log file", "file", logFile, "error", err)
} else {
logHandler = slogmulti.Fanout(
tint.NewHandler(os.Stdout, consoleOpts),
tint.NewHandler(logFH, fileOpts),
handler = slogmulti.Fanout(
tint.NewHandler(os.Stdout, generateOptions(logLevel, os.Stdout.Fd())),
tint.NewHandler(logFH, generateOptions(logLevel, logFH.Fd())),
)
}
} else {
handler = slogmulti.Fanout(
tint.NewHandler(os.Stdout, generateOptions(logLevel, os.Stdout.Fd())),
)
}

logger := slog.New(logHandler)
logger := slog.New(handler)

slog.SetDefault(logger)

return logger
}

func generateOptions(level slog.Level, fd uintptr) *tint.Options {
opts := &tint.Options{
Level: level,
NoColor: !isatty.IsTerminal(fd),
}
if level == LevelTrace {
opts.AddSource = true
}

return opts
}

//nolint:unused
func levelReplacer(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.LevelKey {
level, ok := attr.Value.Any().(slog.Level)
Expand Down

0 comments on commit a3e05bb

Please sign in to comment.