cmd: flush buffered logs before exiting on command error [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] - #7963
Open
waterWang wants to merge 1 commit into
Open
Conversation
When a config fails to load, cmdRun flushes the log buffer and returns the error. WrapCommandFuncForCobra then logs that error via caddy.Log(), which is still the buffered logger (the config never loaded to replace it), so the error is written into the buffer and then lost when Main() exits the process without ever flushing it. Add caddy.FlushLogs(), which flushes any buffered log entries to the original default logger's output, and call it from WrapCommandFuncForCobra's error path so startup/config errors are not silently dropped. Fixes caddyserver#7962
|
|
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
Fixes #7962 — config/startup errors are silently dropped because they are logged through the buffered default logger and never flushed before the process exits.
Root cause
When a config fails to load,
cmdRun(incmd/commandfuncs.go) flushes the log buffer vialogBuffer.FlushTo(defaultLogger)and returns the error.WrapCommandFuncForCobra(incmd/cobra.go) then logs that error withcaddy.Log(). At that pointcaddy.Log()still returns the buffered logger, because the config never loaded and thus never replaced the default logger with the configured one. The error is written into the buffer and lost whenMain()callsos.Exit— nothing flushes the buffer afterwards.Fix
caddy.FlushLogs()which flushes any buffered log entries to the original default logger's output (no-op when the default logger is not buffered).BufferedLog()now remembers the original logger so it can be flushed later.caddy.FlushLogs()fromWrapCommandFuncForCobra's error path, right after the error is logged, so the error is actually written to stderr before the process exits.Test
TestFlushLogsverifies: (1) entries logged through the buffered logger are not written immediately, (2)FlushLogswrites them to the original logger's output, (3) a secondFlushLogsis a no-op (the buffer was drained, entry not duplicated).Verification
go vet ./ ./cmd/passesgo test ./ -run TestFlushLogspassesgo test ./cmd/ -run TestCommandsAreAvailablepassesAI disclosure
This contribution was prepared with assistance from an AI agent (Hermes Agent by Nous Research). The change, test, and this description were reviewed by the human author, who can explain every line. Full build+run verification was limited on the author's machine by network/TLS constraints; unit tests above pass locally.
Signed-off-by: waterWang [email protected]