From 6b5fa336331a8cfa0d325632bea6fd60871cb747 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Thu, 10 Apr 2025 09:56:47 +0200 Subject: [PATCH] debug log level is dynamic and set after Logger creation So we should use dynamic handler to set the log level after. With this patch we can clearly see the output. Before we were always stuck in log level "info" and not seeing debug log level ``` bin/helm upgrade --install --debug --wait frontend \ --namespace test \ --set replicaCount=2 \ --set backend=http://backend-podinfo:9898/echo \ podinfo/podinfo level=DEBUG msg="getting history for release" release=frontend level=DEBUG msg="preparing upgrade" name=frontend level=DEBUG msg="performing update" name=frontend level=DEBUG msg="creating upgraded release" name=frontend level=DEBUG msg="checking resources for changes" resources=2 level=DEBUG msg="no changes detected" kind=Service name=frontend-podinfo level=DEBUG msg="patching resource" kind=Deployment name=frontend-podinfo namespace=test level=DEBUG msg="waiting for resources" count=2 timeout=5m0s level=DEBUG msg="waiting for resource" name=frontend-podinfo kind=Deployment expectedStatus=Current actualStatus=Unknown level=DEBUG msg="updating status for upgraded release" name=frontend Release "frontend" has been upgraded. Happy Helming! NAME: frontend LAST DEPLOYED: Thu Apr 10 09:56:25 2025 NAMESPACE: test STATUS: deployed REVISION: 6 DESCRIPTION: Upgrade complete ``` Signed-off-by: Benoit Tigeot --- pkg/cli/logger.go | 56 +++++++++++++++--- pkg/cmd/root.go | 2 +- .../issue-7233/charts/alpine-0.1.0.tgz | Bin 0 -> 1167 bytes 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 pkg/cmd/testdata/testcharts/issue-7233/charts/alpine-0.1.0.tgz 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 0000000000000000000000000000000000000000..afd021846ed6a2f7cc2cf023ed188a7cf2f9d189 GIT binary patch literal 1167 zcmV;A1aSKwiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PI-XZsRr+&b6LmpuJto@*y$Scfr34{EN1WMOrLS6h%Fj#ugEZ zR7uK;>-D=AJjjWaZc?CYrw7fAAd37rGn{W`DC89rH2hzI$|PGX`Nh|lG)>d1>C`>b zH0?gq@#W>kXgZrsXS2~by}C$8dd9SW<{}927eIliq6m!^& zBCM*zYdlHb#8FN-#>=q*)TZUJG5nq_e9f(O23qP~Ml=20O_nnPhsrRT$8LA*?K z;hvE|`^kq}q-Cu#((`C=n7n4DsFz75OE=#y+O)c)$tX#qmv+{_Py+uq$ZOIkN&wIC zKOLuC{?F2@p8w~N4~~}Qb`Y5P()#prUJ3j+R8|}f>7gGOR5Jf++29%ek0;Y{hyRz; zY0v+&NT>eaGLg^Wqs*g{4CZKX9s&5;9q)F@4RJzEiA@{({b09CKKaVw2jU2T4eE)i2~P@50=~5F94>Y)|7*hU=(Jz&=f2yz(~mhRPLG& z$^l``HY6Z(O)I=NVezWwu#yTeFPYHL6cQQ~#zJZ$XbLm|N_jIhAXKOf%W96w?PZ}9 z=}HRCmYghJ;ubw+!yF#C=6g~bmJxi0Uu$Uy_WP$@!Gty_GKwLSVnf1qT2SIGX5n!u#05lSibC4@A1;IB5RBM25u)q{(pdm$&DMDkNr=7`uRo5Y3GPTw5$WVLa zT`M0iJ+yGU9VGsmaeZhqA3BHW$5vyVGvm)0YK`llVB1)_4?ZwG@_ktP_ppr*OaR~I zI3te2HqsSkHe!Pwx{!^ALN->1T3gR+R#u!mLgHsNjC0^p-uj?}3bm$uz=WUW;4+tHl)A88ky z+)*+DyRAVcNVz;Q2nnV^W=Oe{VkNF^%JJ1`{)O1_r<%#KM4PsLiib-khME&q@$2|a znx^s3eMj@8g!+H;?)vR_?~*b<#U9V~|Cd*@ZvUT-`}_Y{H$l1UW(C~L@2fGduigL&u(Z@M?PosbL<7QghdA0+Uf?u^1;PV^Vx z+57)w(&7JfG`j5he-1IEjjh4{KY$B^fe(YfPmK0*Itl!@&ETo%|0nq0z5h>UlS$A2 hbI39OZ5Z_Q@1>VsdigKN?*RY+|NrPaN*Dkj008|nJ|6%8 literal 0 HcmV?d00001