Skip to content

Honor stderrthreshold when logtostderr is enabled#9654

Merged
blackpiglet merged 2 commits intovelero-io:mainfrom
pierluigilenoci:fix/honor-stderrthreshold
Apr 24, 2026
Merged

Honor stderrthreshold when logtostderr is enabled#9654
blackpiglet merged 2 commits intovelero-io:mainfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown
Contributor

@pierluigilenoci pierluigilenoci commented Mar 26, 2026

What this PR does

Fixes the -stderrthreshold flag so it is honored even when -logtostderr=true (the klog v2 default).

Problem

klog v2 defaults -logtostderr to true. When this flag is active, the -stderrthreshold flag is silently ignored — all log messages (INFO, WARNING, ERROR, FATAL) are unconditionally written to stderr. There is no way for users to filter which severity levels reach stderr.

This has been an open issue since 2020: kubernetes/klog#212.

Fix

PR kubernetes/klog#432 introduced the fix in klog v2.140.0 via a new flag legacy_stderr_threshold_behavior. This PR:

  1. Bumps klog from v2.130.1 to v2.140.0
  2. Opts into the fixed behavior by setting legacy_stderr_threshold_behavior=false and stderrthreshold=INFO after klog.InitFlags()

Setting stderrthreshold=INFO preserves the current default behavior (all logs go to stderr). Users can now override it on the command line (e.g., -stderrthreshold=WARNING) and it will actually work.

Changes

  • pkg/cmd/velero/velero.go: Add flag.CommandLine.Set() calls after klog.InitFlags(flag.CommandLine)
  • go.mod / go.sum: Bump k8s.io/klog/v2 v2.130.1 → v2.140.0

Ref: kubernetes/klog#212, kubernetes/klog#432

@github-actions github-actions Bot requested review from Lyndon-Li and sseago March 26, 2026 23:11
@github-actions github-actions Bot added the Dependencies Pull requests that update a dependency file label Mar 26, 2026
@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Hi @Lyndon-Li, @skriss — gentle ping. Would you be able to review this when you get a chance? Thank you!

@kaovilai
Copy link
Copy Markdown
Collaborator

kaovilai commented Mar 28, 2026

Please create an issue if this was a bug/regression. And please run make new-changelog

Please include some examples of new behaviors vs old.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 28, 2026

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/cmd/velero/velero.go 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

@kaovilai Thanks for the feedback! I've created issue #9658 for this.

Old vs New Behavior

Before this PR (klog < v2.140.0 or legacy mode)

$ velero server --stderrthreshold=WARNING
# stderr output:
I0328 10:00:00.000000   12345 server.go:100] Starting Velero server   ← INFO appears on stderr (unexpected)
W0328 10:00:01.000000   12345 server.go:200] Deprecation warning       ← WARNING on stderr (expected)
E0328 10:00:02.000000   12345 server.go:300] Connection failed          ← ERROR on stderr (expected)

The --stderrthreshold=WARNING flag is silently ignored — all levels including INFO are written to stderr because --logtostderr=true bypasses the threshold check entirely.

After this PR

$ velero server --stderrthreshold=WARNING
# stderr output:
W0328 10:00:01.000000   12345 server.go:200] Deprecation warning       ← WARNING on stderr (expected)
E0328 10:00:02.000000   12345 server.go:300] Connection failed          ← ERROR on stderr (expected)

The --stderrthreshold flag now works correctly. Only messages at or above the threshold level appear on stderr.

Default behavior (unchanged)

With the default --stderrthreshold=INFO, the output is identical to before — all log levels appear on stderr. This PR only changes behavior for users who explicitly set a higher threshold.

See: kubernetes/klog#432 for the upstream fix details.

Copy link
Copy Markdown
Collaborator

@kaovilai kaovilai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Responses generated with Claude

Comment thread pkg/cmd/velero/velero.go Outdated
klog.InitFlags(flag.CommandLine)
// Opt into fixed stderrthreshold behavior (kubernetes/klog#212).
_ = flag.Set("legacy_stderr_threshold_behavior", "false")
_ = flag.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider adding a comment clarifying that this sets the default, but users can still override it via --stderrthreshold on the command line since flag parsing happens later at Execute() time.

Note

Responses generated with Claude

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll add a clarifying comment. The flag.Set call here sets the programmatic default so that --stderrthreshold=ERROR is the baseline, but since this runs before cmd.Execute() invokes pflag parsing, any explicit --stderrthreshold= on the command line will override it as expected.

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Thanks @kaovilai! I'll create an issue and add the changelog entry.

Behavior comparison:

Scenario Before (broken) After (fixed)
Default (--logtostderr=true, --stderrthreshold=ERROR) All log levels go to stderr (threshold ignored) All log levels go to stderr (same — no behavior change)
User sets --stderrthreshold=WARNING Still all levels go to stderr (flag silently ignored) Only WARNING and ERROR go to stderr (flag honored)
User sets --stderrthreshold=ERROR Still all levels go to stderr (flag silently ignored) Only ERROR goes to stderr (flag honored)

The key point: default behavior does not change. The fix only matters when users explicitly set --stderrthreshold to filter log output — which was previously impossible despite the flag existing.

I'll push the changelog entry shortly and create the upstream issue.

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

@kaovilai Done! Here's what I've added:

The codecov/patch failure is expected — the two new flag.Set() lines in velero.go aren't covered by unit tests, but they're simple flag initialization that's already tested via integration in klog itself.

Is there anything else you'd like me to address?

@blackpiglet blackpiglet force-pushed the fix/honor-stderrthreshold branch from 2826fc5 to 323021c Compare April 2, 2026 08:36
@blackpiglet
Copy link
Copy Markdown
Contributor

@pierluigilenoci
Will the --legacy_stderr_threshold_behavior parameter be automatically included in the Velero CLI as a result of this PR?

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

@blackpiglet Good question. Yes, --legacy_stderr_threshold_behavior is already exposed as a Velero CLI flag — both before and after this PR. It is not new to this change.

Here is why: Velero calls klog.InitFlags(flag.CommandLine) in NewCommand() (see pkg/cmd/velero/velero.go), which registers all klog flags into Go's standard flag.CommandLine. The command then adds them to the Cobra command via c.PersistentFlags().AddGoFlagSet(flag.CommandLine). Since klog registers legacy_stderr_threshold_behavior as a boolean flag in its init() function (see klog.go), it becomes available on the Velero CLI as --legacy_stderr_threshold_behavior.

What this PR changes is the programmatic default: it calls flag.Set("legacy_stderr_threshold_behavior", "false") before flag parsing, so the fixed behavior is active by default without users needing to pass the flag explicitly. Users can still override it with --legacy_stderr_threshold_behavior=true on the command line if they need the old behavior.

In summary:

  • The flag was already present in the Velero CLI (inherited from klog via InitFlags).
  • This PR flips its effective default from true to false so that --stderrthreshold is honored when --logtostderr=true.
  • No new flags are introduced by this PR; the behavior change comes from setting a different default for an existing klog flag.

@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

@blackpiglet Could you please approve the pending CI workflow runs when you get a chance? All checks (PR CI Check, PR Linter Check, PR Changelog Check, PR Codespell Check, E2E test) are currently in action_required state, waiting for maintainer approval to run on this fork PR. Thank you!

@blackpiglet blackpiglet force-pushed the fix/honor-stderrthreshold branch from 8330653 to d556efb Compare April 13, 2026 05:43
blackpiglet
blackpiglet previously approved these changes Apr 13, 2026
@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Thank you @blackpiglet for the review and approval! All CI checks should be green now — could this be merged when convenient? Happy to address anything else if needed.

@blackpiglet
Copy link
Copy Markdown
Contributor

@pierluigilenoci
Merging needs two approvals.
Let's wait for other maintainers' input.

@blackpiglet
Copy link
Copy Markdown
Contributor

Sorry, clicked the close button accidentally.

klog v2 defaults -logtostderr to true, which silently ignores the
-stderrthreshold flag — all log levels are unconditionally sent to
stderr. This makes it impossible for log-aggregation systems to filter
by severity.

Bump klog to v2.140.0 and opt into the fixed behavior by setting
legacy_stderr_threshold_behavior=false and stderrthreshold=INFO (which
preserves current output while letting users override via CLI flags).

Ref: kubernetes/klog#212, kubernetes/klog#432
Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@pierluigilenoci
Copy link
Copy Markdown
Contributor Author

Hi — friendly ping. Is this PR still on the radar for a second review? Happy to rebase or make changes if needed. Thanks!

blackpiglet
blackpiglet previously approved these changes Apr 22, 2026
@blackpiglet
Copy link
Copy Markdown
Contributor

Hi — friendly ping. Is this PR still on the radar for a second review? Happy to rebase or make changes if needed. Thanks!

Yes, approved.
Still need one more vote from the maintainers.

Add missing changelog entry for PR 9654 (fixes Changelog Check).
Add explanation to //nolint:errcheck directives (fixes nolintlint).

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@blackpiglet blackpiglet merged commit f30be4b into velero-io:main Apr 24, 2026
56 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Dependencies Pull requests that update a dependency file has-changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants