Skip to content

Revisit Logger::flush, Append::flush, and Async shutdown guard ahead of 1.0 #338

Backlog

Description

@tisonkun

Context

Follow-up to #328 and #335.

While applying the assembly-owned sink flush pattern from #328 and #335 to a multi-channel production service (general, statement, ingest, compaction, and metering), we audited what Append::flush and Logger::flush actually do across all 10 built-in Append implementations in Logforth:

  1. 7 of 10 built-in appenders perform no user-space buffering at all:
    • Testing, Journald (UnixDatagram), and PosixSyslog (libc::syslog) have literal Ok(()) flush() implementations.
    • Stderr wraps unbuffered std::io::Stderr (StderrRaw::flush is Ok(())).
    • Stdout appends b'\n' before write_all, which already flushes std::io::Stdout's LineWriter on every record.
    • File (RollingFileWriter) writes directly to an unbuffered std::fs::File, whose Write::flush in std is a no-op Ok(()).
    • SocketSyslog uses unbuffered datagram/stream sockets, and TcpTlsSender::send already calls write_all(...).and_then(|()| stream.flush()) per frame.
  2. 2 of 10 built-in appenders delegate to external telemetry runtimes whose lifecycles live outside Logger:
    • FastraceEvent::flush calls global fastrace::flush(), coupling a per-span event appender to the process-wide trace collector lifecycle.
    • OpentelemetryLog::flush calls SdkLoggerProvider::force_flush(), whereas callers already construct and retain SdkLoggerProvider to call provider.shutdown() at exit (and sharing one provider across multiple OpentelemetryLog scopes triggers redundant force_flush() calls when driven via Append::flush).
  3. Async is the only built-in appender with an internal Logforth buffer, and its current Drop lifecycle breaks under static loggers:
    • Async currently owns JoinHandle<()> directly, making Async !Clone (requiring Arc<Async> for multi-logger sharing).
    • When Logger instances are stored in OnceLock / LazyLock statics (as in examples/src/multi_logger_pipeline.rs) or installed globally via LogBridge::install(), Async::drop never runs at process exit, so the background worker thread is never joined on shutdown.

Candidate Redesign Ahead of 1.0

Separating Async into a Clone sender handle plus an RAII #[must_use] AsyncGuard (AsyncBuilder::build(self) -> Result<(Async, AsyncGuard), Error>, analogous to tracing_appender::non_blocking's (NonBlocking, WorkerGuard)) lets main deterministically drain the queue and join the worker thread on exit even when Logger handles live in statics.

With AsyncGuard and SdkLoggerProvider owning async/batch teardown at the initialization boundary, we can evaluate two stacked steps for logforth-core:

  1. Step 1 (Plan B — Remove Logger::flush, keep defaulted Append::flush):
    • Remove Logger::flush so Logger is strictly a non-owning emission handle (enabled, log), matching Go log/slog, SLF4J, .NET ILogger, and tracing (LogBridge's log::Log::flush becomes a no-op like tracing-log).
    • Split Async into (Async, AsyncGuard) (Async: Clone, AsyncGuard::flush(&self) + deterministic shutdown/join on Drop).
    • Default Append::flush(&self) -> Result<(), Error> to Ok(()), dropping no-op and cross-signal flush implementations (Testing, Stderr, File, Journald, PosixSyslog, FastraceEvent) while preserving Async's ability to flush custom buffered inner Box<dyn Append> implementations.
  2. Step 2 (Plan A — Also remove Append::flush):
    • Remove Append::flush completely so Append becomes a single-method trait (fn append(&self, record: &Record, diags: &dyn Diagnostic) -> Result<(), Error>).
    • Synchronous appenders write complete records per append call; any buffered, asynchronous, or batching sink (AsyncGuard, SdkLoggerProvider, or a custom batching sink's own guard) manages flushing and shutdown explicitly via RAII drop order in main.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions