diff --git a/pkg/cli/logger.go b/pkg/cli/logger.go index d75622c37..03a69be24 100644 --- a/pkg/cli/logger.go +++ b/pkg/cli/logger.go @@ -17,19 +17,53 @@ limitations under the License. package cli import ( + "context" "log/slog" "os" ) -func NewLogger(debug bool) *slog.Logger { - level := slog.LevelInfo - if debug { - level = slog.LevelDebug +// DebugCheckHandler checks settings.Debug at log time +type DebugCheckHandler struct { + handler slog.Handler + settings *EnvSettings +} + +// Enabled implements slog.Handler.Enabled +func (h *DebugCheckHandler) Enabled(_ context.Context, level slog.Level) bool { + if level == slog.LevelDebug { + return h.settings.Debug // Check settings.Debug at log time } + return true // Always log other levels +} - // Create a handler that removes timestamps - handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ - Level: level, +// Handle implements slog.Handler.Handle +func (h *DebugCheckHandler) Handle(ctx context.Context, r slog.Record) error { + return h.handler.Handle(ctx, r) +} + +// WithAttrs implements slog.Handler.WithAttrs +func (h *DebugCheckHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &DebugCheckHandler{ + handler: h.handler.WithAttrs(attrs), + settings: h.settings, + } +} + +// WithGroup implements slog.Handler.WithGroup +func (h *DebugCheckHandler) WithGroup(name string) slog.Handler { + return &DebugCheckHandler{ + handler: h.handler.WithGroup(name), + settings: h.settings, + } +} + +// NewLogger creates a new logger with dynamic debug checking +func NewLogger(settings *EnvSettings) *slog.Logger { + // Create base handler that removes timestamps + baseHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + // Always use LevelDebug here to allow all messages through + // Our custom handler will do the filtering + Level: slog.LevelDebug, ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { // Remove the time attribute if a.Key == slog.TimeKey { @@ -39,5 +73,11 @@ func NewLogger(debug bool) *slog.Logger { }, }) - return slog.New(handler) + // Wrap with our dynamic debug-checking handler + dynamicHandler := &DebugCheckHandler{ + handler: baseHandler, + settings: settings, + } + + return slog.New(dynamicHandler) } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 0cbcfebaf..cbef840b3 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -95,7 +95,7 @@ By default, the default directories depend on the Operating System. The defaults ` var settings = cli.New() -var Logger = cli.NewLogger(settings.Debug) +var Logger = cli.NewLogger(settings) func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) { actionConfig := new(action.Configuration) diff --git a/pkg/cmd/testdata/testcharts/issue-7233/charts/alpine-0.1.0.tgz b/pkg/cmd/testdata/testcharts/issue-7233/charts/alpine-0.1.0.tgz new file mode 100644 index 000000000..afd021846 Binary files /dev/null and b/pkg/cmd/testdata/testcharts/issue-7233/charts/alpine-0.1.0.tgz differ