diff --git a/src/cmd/root.go b/src/cmd/root.go index cb01467362..3d045ab177 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -197,7 +197,7 @@ func setupLogger(level, format string, color bool) (*slog.Logger, error) { Level: sLevel, Format: logger.Format(format), Destination: logger.DestinationDefault, - Color: color, + Color: logger.Color(color), } l, err := logger.New(cfg) if err != nil { diff --git a/src/pkg/logger/logger.go b/src/pkg/logger/logger.go index 86f2ca670c..a125888313 100644 --- a/src/pkg/logger/logger.go +++ b/src/pkg/logger/logger.go @@ -131,16 +131,19 @@ type Config struct { Level Format Destination - Color bool + Color } +// Color is a type that represents whether or not to use color in the logger. +type Color bool + // LogValue of config func (c Config) LogValue() slog.Value { return slog.GroupValue( slog.String("level", c.Level.String()), slog.Any("format", c.Format), slog.Any("destination", destinationString(c.Destination)), - slog.Bool("color", c.Color), + slog.Bool("color", bool(c.Color)), ) } @@ -175,7 +178,7 @@ func New(cfg Config) (*slog.Logger, error) { case FormatConsole: handler = console.NewHandler(cfg.Destination, &console.HandlerOptions{ Level: slog.Level(cfg.Level), - NoColor: !cfg.Color, + NoColor: !bool(cfg.Color), }) case FormatJSON: handler = slog.NewJSONHandler(cfg.Destination, &opts) @@ -188,7 +191,7 @@ func New(cfg Config) (*slog.Logger, error) { handler = devslog.NewHandler(DestinationDefault, &devslog.Options{ HandlerOptions: &opts, NewLineAfterLog: true, - NoColor: !cfg.Color, + NoColor: !bool(cfg.Color), }) // Use discard handler if no format provided case "", FormatNone: diff --git a/src/pkg/logger/logger_test.go b/src/pkg/logger/logger_test.go index eaaf54ef47..4aab4d73db 100644 --- a/src/pkg/logger/logger_test.go +++ b/src/pkg/logger/logger_test.go @@ -76,7 +76,7 @@ func Test_New(t *testing.T) { }, }, { - name: "color can be true", + name: "color true is ok", cfg: Config{ Color: true, },