Skip to content

fix: honor klog -stderrthreshold even when -logtostderr is true#164

Merged
johnjeffers merged 2 commits into
luxury-yacht:mainfrom
pierluigilenoci:fix/honor-stderrthreshold
Apr 24, 2026
Merged

fix: honor klog -stderrthreshold even when -logtostderr is true#164
johnjeffers merged 2 commits into
luxury-yacht:mainfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown
Contributor

What changed

klog v2 defaults -logtostderr to true, which silently ignores -stderrthreshold — all log levels go to stderr unconditionally. This has been an open issue since 2020.

klog v2.140.0 (already in use by this project) introduced a fix behind an opt-in flag (legacy_stderr_threshold_behavior). This PR enables the fix in backend/internal/errorcapture/error_capture.go so that the stderrthreshold setting is actually honored when -logtostderr=true.

What the fix does

klog.InitFlags(klogFlags)
klogFlags.Set("legacy_stderr_threshold_behavior", "false") // enable the klog fix
klogFlags.Set("stderrthreshold", "INFO")                   // preserve current default

The fix is backward-compatible and preserves the current stderr capture behavior.

References

klog v2 defaults -logtostderr to true, which silently ignores
-stderrthreshold — all log levels go to stderr unconditionally.
This has been an open issue since 2020 (kubernetes/klog#212).

klog v2.140.0 introduced a fix behind an opt-in flag
(legacy_stderr_threshold_behavior). This commit enables the fix
so that -stderrthreshold is honored, while preserving the current
default behavior (stderrthreshold=INFO means all logs still go to
stderr unless the user overrides it on the command line).

Ref: kubernetes/klog#212
Ref: kubernetes/klog#432
Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
klogFlags.Set("legacy_stderr_threshold_behavior", "false")
klogFlags.Set("stderrthreshold", "INFO")
klogFlags.Set("logtostderr", "true")
klogFlags.Set("stderrthreshold", "0")
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.

Should probably remove either this line or line 75, since they appear to do the same thing.

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 catch — the "INFO" form on line 75 and the numeric "0" on line 77 resolve to the same klog severity, so the second set was a leftover from the original code that I forgot to remove when I added the named constant. Removed the duplicate in 4a18301.

The "INFO" string form (line 75) and the numeric "0" form were both
setting the same klog severity level. Keep the human-readable "INFO"
that sits next to the explanatory comment and drop the redundant "0".

Signed-off-by: Pierluigi Lenoci <pierluigi.lenoci@gmail.com>
Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@johnjeffers johnjeffers merged commit 9eff6e3 into luxury-yacht:main Apr 24, 2026
@johnjeffers
Copy link
Copy Markdown
Collaborator

@pierluigilenoci thank you for the contribution! This will go out in v1.6.2, likely later today, possibly tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants