fix: honor klog -stderrthreshold even when -logtostderr is true#1013
Open
pierluigilenoci wants to merge 1 commit intomongodb:masterfrom
Open
fix: honor klog -stderrthreshold even when -logtostderr is true#1013pierluigilenoci wants to merge 1 commit intomongodb:masterfrom
pierluigilenoci wants to merge 1 commit intomongodb:masterfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
klog v2 defaults
-logtostderrtotrue, which silently ignores-stderrthreshold— all log levels go to stderr unconditionally. This has been an open issue since 2020.klog v2.140.0 introduced a fix behind an opt-in flag (
legacy_stderr_threshold_behavior). This PR enables the fix inmain.goso that-stderrthresholdis honored, while preserving the current default behavior (stderrthreshold=INFOmeans all logs still go to stderr unless the user explicitly overrides it on the command line).Why this matters
In production Kubernetes clusters, log-aggregation systems (Fluentd, Fluent Bit, Loki, Datadog, …) typically ingest from container stderr. When every severity goes there indiscriminately:
The
-stderrthresholdflag exists precisely to solve this, but it has never worked under the default klog v2 configuration — until now.What the fix does
klog.InitFlags(nil)(registers the flags) but before the flags are usedstderrthreshold=INFO: keeps current behavior — all logs still go to stderr by default-stderrthreshold=WARNINGor-stderrthreshold=ERRORand it will actually take effectReferences