Skip to content

Commit

Permalink
Resolved bug that incorrectly defaulted to JSON output unless color l…
Browse files Browse the repository at this point in the history
…ogger was used
  • Loading branch information
l50 committed Oct 21, 2024
1 parent 6b62acc commit d1a764d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions logging/logutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (cfg *LogConfig) ConfigureLogger() (Logger, error) {
var err error
var fileHandler slog.Handler
var stdoutHandler slog.Handler

opts := &slog.HandlerOptions{
Level: cfg.Level,
}
Expand All @@ -113,11 +112,14 @@ func (cfg *LogConfig) ConfigureLogger() (Logger, error) {
fileHandler = slog.NewJSONHandler(logFile, opts)
}

if cfg.OutputType == ColorOutput {
switch cfg.OutputType {
case ColorOutput:
prettyOpts := PrettyHandlerOptions{SlogOpts: *opts}
stdoutHandler = NewPrettyHandler(os.Stdout, prettyOpts, cfg.OutputType)
} else {
case JSONOutput:
stdoutHandler = slog.NewJSONHandler(os.Stdout, opts)
default: // PlainOutput
stdoutHandler = slog.NewTextHandler(os.Stdout, opts)
}

var handlers []slog.Handler
Expand All @@ -127,11 +129,9 @@ func (cfg *LogConfig) ConfigureLogger() (Logger, error) {
if stdoutHandler != nil {
handlers = append(handlers, stdoutHandler)
}

if len(handlers) == 0 {
return nil, fmt.Errorf("no valid handlers available for logger")
}

multiHandler := slog.New(slogmulti.Fanout(handlers...))
var logger Logger
if cfg.OutputType == ColorOutput {
Expand All @@ -147,7 +147,6 @@ func (cfg *LogConfig) ConfigureLogger() (Logger, error) {
Logger: multiHandler,
}
}

return logger, nil
}

Expand Down

0 comments on commit d1a764d

Please sign in to comment.