Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cmd: flush buffered logs before exiting on command error [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] - #7963

Open
waterWang wants to merge 1 commit into
caddyserver:masterfrom
waterWang:fix-flush-buffered-error-logs
Open

cmd: flush buffered logs before exiting on command error [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT]#7963
waterWang wants to merge 1 commit into
caddyserver:masterfrom
waterWang:fix-flush-buffered-error-logs

Conversation

@waterWang

@waterWang waterWang commented Aug 28, 2026

Copy link
Copy Markdown

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 (in cmd/commandfuncs.go) flushes the log buffer via logBuffer.FlushTo(defaultLogger) and returns the error. WrapCommandFuncForCobra (in cmd/cobra.go) then logs that error with caddy.Log(). At that point caddy.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 when Main() calls os.Exit — nothing flushes the buffer afterwards.

Fix

  • Add 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.
  • Call caddy.FlushLogs() from WrapCommandFuncForCobra's error path, right after the error is logged, so the error is actually written to stderr before the process exits.

Test

  • New TestFlushLogs verifies: (1) entries logged through the buffered logger are not written immediately, (2) FlushLogs writes them to the original logger's output, (3) a second FlushLogs is a no-op (the buffer was drained, entry not duplicated).

Verification

  • go vet ./ ./cmd/ passes
  • go test ./ -run TestFlushLogs passes
  • go test ./cmd/ -run TestCommandsAreAvailable passes

AI 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]

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
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

Flush error logs before process exits

2 participants