From d1a764d9d3cd6bc09450e8f8d690c32f38f0a960 Mon Sep 17 00:00:00 2001 From: Jayson Grace Date: Sun, 20 Oct 2024 22:23:03 -0600 Subject: [PATCH] Resolved bug that incorrectly defaulted to JSON output unless color logger was used --- logging/logutils.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/logging/logutils.go b/logging/logutils.go index 2625063..293663f 100644 --- a/logging/logutils.go +++ b/logging/logutils.go @@ -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, } @@ -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 @@ -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 { @@ -147,7 +147,6 @@ func (cfg *LogConfig) ConfigureLogger() (Logger, error) { Logger: multiHandler, } } - return logger, nil }