Skip to content

refactor!: remove Logger::flush, add AsyncGuard, and default Append::flush - #339

Draft
tisonkun wants to merge 1 commit into
mainfrom
refactor/remove-logger-flush
Draft

tisonkun wants to merge 1 commit into
mainfrom
refactor/remove-logger-flush

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Note

PR Stack (#338 exploration)

  1. This PR (Option B): Remove Logger::flush, split AsyncBuilder::build into (Async, AsyncGuard), and default Append::flush to Ok(()).
  2. refactor(core)!: remove Append::flush #340 — Option A (stacked on this PR): Remove Append::flush from Append altogether.

Refs #338.

Problem

  1. Logger::flush conflates event emission with sink lifecycle ownership: When multiple channel Loggers share an underlying Async or OpentelemetryLog sink, calling flush() on every Logger triggers redundant worker barriers or network export RPCs, while callers holding only a Logger may flush shared or global resources (for example, FastraceEvent::flush previously flushed global fastrace trace spans).
  2. Async could not shut down deterministically behind static loggers: Because Rust does not run Drop on static items (OnceLock<Logger>, LazyLock<Logger>, or LogBridge::install()), storing JoinHandle<()> directly inside Async prevented Async from implementing Clone and caused Async::drop (worker drain + join) to be skipped at process exit unless callers manually wired custom shutdown plumbing.
  3. Most Append implementations have no user-space buffer to flush: Requiring every Append implementor to write fn flush(&self) -> Result<(), Error> forced boilerplate Ok(()) implementations across unbuffered sinks (Testing, Stderr, File, Journald, PosixSyslog).

Solution

  • Remove Logger::flush: Logger is strictly a lightweight, non-owning emission handle. LogBridge's log::Log::flush implementation becomes a no-op {} (matching tracing-log), while explicit sink lifecycle handles manage flushing and shutdown.
  • Introduce #[must_use] AsyncGuard from AsyncBuilder::build (Result<(Async, AsyncGuard), Error>):
    • Async becomes Clone directly without wrapping in Arc<Async>, allowing multiple Dispatches or Loggers to share one background worker thread naturally.
    • AsyncGuard exposes AsyncGuard::flush(&self) -> Result<(), Error> for on-demand synchronous queue draining and deterministically drains queued records, flushes inner appenders, and joins the worker thread on Drop (including safe non-blocking handling if dropped re-entrantly on the worker thread itself). Binding _guard in main guarantees clean drain on exit even when Logger handles live in statics.
  • Default Append::flush to Ok(()) and prune no-op / cross-signal overrides:
    • Append::flush defaults to Ok(()) so unbuffered appenders (Testing, Stderr, File, Journald, PosixSyslog, and FastraceEvent) omit flush boilerplate, and FastraceEvent no longer triggers global fastrace::flush() trace export as a side effect of log flushing.
    • Stdout, SocketSyslog, OpentelemetryLog, Async, Arc<T>, and &'static T continue forwarding or implementing Append::flush.
  • Update examples, tests, and crate READMEs: Simplify examples/src/multi_logger_pipeline.rs and README snippets to bind _guard: AsyncGuard (or provider.shutdown()) at the application entry point instead of calling logger.flush().

…flush

Remove `Logger::flush` so `Logger` acts strictly as a non-owning event emission handle (`LogBridge`'s `log::Log::flush` becomes a no-op). Split `AsyncBuilder::build` into `(Async, AsyncGuard)` so `Async` implements `Clone` directly while `AsyncGuard` deterministically drains queued records and joins the worker thread on `Drop` even when loggers live in `static`s. Provide a default `Ok(())` body on `Append::flush` and drop no-op and cross-signal `flush` overrides.

Signed-off-by: tison <wander4096@gmail.com>
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.

1 participant