-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate pkg to slog #30708
Migrate pkg to slog #30708
Conversation
dca38af
to
df264d2
Compare
a4e9552
to
359f585
Compare
As for helm v4. We want to migrate logs to slog. Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
``` helmClient.Log = NewSlogAdapter(slog.Default()) ``` Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
See: helm#30698 (comment) Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
0edab58
to
b238072
Compare
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pkg/cli/logger.go
Outdated
} | ||
|
||
// Create a handler that removes timestamps | ||
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use color for error or not https://betterstack.com/community/guides/logging/logging-in-go/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am more curious why add the logger rather than just use slog directly? I thought I mentioned that on another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we wanted to chose extactly the output, like do not have the timestamp. Also to handle debug loglevel like it was done before it seems we need to have a custom slog Handler
that handle the debug flag just before log is processed.
6e684c8
to
bf3d011
Compare
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
bf3d011
to
710770e
Compare
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
87846ec
to
0c85456
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First let me thank you for all your hard work on this. Where we are calling logging looks good but how we implement the logging i think needs to change. Rather than passing around a logger Log
as to just call slog
directly. Would love to setup some time to disucss if you are up to it.
pkg/cli/logger.go
Outdated
} | ||
|
||
// Create a handler that removes timestamps | ||
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am more curious why add the logger rather than just use slog directly? I thought I mentioned that on another PR.
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 <benoit.tigeot@lifen.fr>
I will ping you on Slack. ;) |
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
@@ -148,6 +140,9 @@ func newRootCmdWithConfig(actionConfig *action.Configuration, out io.Writer, arg | |||
settings.AddFlags(flags) | |||
addKlogFlags(flags) | |||
|
|||
logger := logging.NewLogger(func() bool { return settings.Debug }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to be dynamic with the settings because it is set later. Maybe we could simplify, if you have an idea maybe.
Some ideas here: https://www.reddit.com/r/golang/comments/15nwnkl/achieve_lshortfile_with_slog/ Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
After a quick call with @robertsirc I removed all mention of
Current output example: » 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="getting release history" name=frontend
level=DEBUG msg="preparing upgrade" name=frontend
level=DEBUG msg="getting last revision" name=frontend
.... Sorry again for the size of the PR. |
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
7bcf82a
to
c05bcbd
Compare
Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, there are some issues that we are going to want to follow up with another PR to fix but since this is part of Helm4, we have some liberties.
@@ -237,24 +238,24 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma | |||
// Check reachability of cluster unless in client-only mode (e.g. `helm template` without `--validate`) | |||
if !i.ClientOnly { | |||
if err := i.cfg.KubeClient.IsReachable(); err != nil { | |||
i.cfg.Log(fmt.Sprintf("ERROR: Cluster reachability check failed: %v", err)) | |||
slog.Error(fmt.Sprintf("cluster reachability check failed: %v", err)) | |||
return nil, errors.Wrap(err, "cluster reachability check failed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, we log this as an error here but then return the error below it, it will be logged again. It could make sense to change this but let me bring this back others before you change anything.
if err != nil { | ||
return errors.Errorf("Failed to close reader for pod: %s, container: %s", podName, containerName) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, this was unreachable code so it's safe to remove.
What this PR does / why we need it:
tl;dr : migrate packages and cmd from
log
toslog
for logsWhile trying to implement #30689 but for V4. It was mentionned that I should use
slog
. So I tried to make a first try in#30685, but it requires a change accross packages and cmd.
This PR migrates packages and cmd to
slog
.slog
key-value, with everything in Debug, except logs that start withwarning:
This code still need to be fully manually tested but first I would like to get some feedbacks.
cc @robertsirc @TerryHowe
Close: #30698
Special notes for your reviewer:
If applicable:
docs needed
label should be applied if so)