Skip to content

refactor!: unify Error and ErrorSink without anyhow - #246

Closed
tisonkun wants to merge 3 commits into
mainfrom
refactor/error-sink
Closed

tisonkun wants to merge 3 commits into
mainfrom
refactor/error-sink

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  1. logforth-core depended on anyhow solely for Error::with_source, leaking anyhow::Error into its public signature while returning an unboxed 56-byte Error struct by value across Visitor::visit, Diagnostic::visit, Layout::format, and Append::append.
  2. Runtime pipeline errors followed three disconnected paths:
    • Logger bypassed Trap, wrote multi-line dumps directly to standard error, and panicked if standard error was unavailable.
    • Async forwarded worker errors to Trap.
    • File returned Result for writes and flushes while routing rollover housekeeping and Drop flush errors to a separate FileBuilder::trap.

Solution

  1. Zero-dependency, pointer-sized Error:
    • Box Error's payload (Cow<'static, str> message + optional Box<dyn StdError + Send + Sync + 'static> source) so logforth-core has zero mandatory dependencies and Result<(), Error> fits in a single word (8 bytes on 64-bit targets).
    • Add Error::message(), walk the full .source() chain in Debug, allow with_source to replace an existing cause without panicking, and remove unused with_context, sources, from_io_error, and from_fmt_error helpers in favor of descriptive operation messages at each call site.
  2. Unified error_sink::ErrorSink trait with dispatch-level overrides:
    • Replace trap::Trap and BestEffortTrap with error_sink::ErrorSink (fn sink(&self, err: &Error)), providing zero-sized error_sink::Stderr (default, best-effort without panicking) and error_sink::BlackHole implementations, plus Arc<T> and &'static T forwarding impls for sharing stateful sinks.
    • Configure the logger-wide fallback on LoggerBuilder::error_sink (and LogStarter*Builder::error_sink), allow optional per-pipeline overrides via DispatchBuilder::error_sink, and configure background worker handling on AsyncBuilder::error_sink, removing the hardcoded standard-error panic path in Logger.
    • Remove FileBuilder::trap and RollingFileWriter's Drop callback; when log rotation or archive cleanup fails, finish writing the current record to the active file first and return the rollover error through Append::append to the owning dispatch/logger or async worker.

`logforth-core` previously depended on `anyhow` solely for `Error::with_source`, leaking `anyhow::Error` into its public signature while returning an unboxed 56-byte `Error` struct across hot `Visitor::visit` and `Append::append` paths.

Box the internal error representation into a pointer-sized payload carrying a `Cow<'static, str>` message and an optional `Box<dyn std::error::Error + Send + Sync + 'static>` cause:
- Drop the mandatory `anyhow` dependency from `logforth-core` while preserving `Into` compatibility for standard errors, strings, and boxed causes.
- Shrink `Result<(), Error>` to a single word on the stack, add `Error::message()`, walk the full `.source()` chain in `Debug`, and allow `with_source` to replace causes without panicking.
- Remove unused `with_context`, `sources`, `from_io_error`, and `from_fmt_error` helpers, replacing generic I/O wrappers with descriptive operation messages at each appender call site.
- Expose `logforth_core::error` and `logforth::error` modules directly.

Signed-off-by: tison <wander4096@gmail.com>
Runtime pipeline failures previously followed three disconnected paths: `Logger` wrote multi-line dumps directly to standard error and panicked if standard error was unavailable, `Async` forwarded worker failures to `Trap`, and `File` mixed `Result` returns with a separate `FileBuilder::trap` for rollover housekeeping and `Drop`. In addition, requiring `fmt::Debug` on `Trap` prevented passing closures directly.

Unify internal runtime error handling around a cloneable `error::ErrorSink` value:
- Replace `trap::Trap` and `BestEffortTrap` with `error::ErrorSink` providing `ErrorSink::stderr()` (default, best-effort without panicking), `ErrorSink::black_hole()`, and `ErrorSink::new(...)` / closure `From` conversions.
- Wire `.error_sink(...)` into `LoggerBuilder`, `LogStarterBuilder`, `LogStarterStdStreamBuilder`, `LogStarterTestingBuilder`, and `AsyncBuilder`, removing the hardcoded standard-error panic path in `Logger`.
- Remove `FileBuilder::trap` and `RollingFileWriter`'s `Drop` callback; when log rotation or archive cleanup fails, finish writing the current record to the active file and return the rollover error through `Append::append` to the owning execution root.

Signed-off-by: tison <wander4096@gmail.com>
Align `ErrorSink` with `Append`, `Layout`, `Filter`, and `Diagnostic` by making it a trait (`fmt::Debug + Send + Sync + 'static`) backed by concrete zero-sized implementations instead of a closed enum:
- Introduce `error_sink::ErrorSink` (`fn sink(&self, err: &Error)`), built-in zero-sized `error_sink::Stderr` and `error_sink::BlackHole` implementations, and forwarding impls for `Arc<T>` and `&'static T`.
- Allow per-dispatch overrides via `DispatchBuilder::error_sink` that fall back to `LoggerBuilder::error_sink` when unset.
- Replace the private `BoxedError` type alias in `core/src/error.rs` with `std::error::Error as StdError` and keep `error` private to `logforth-core`.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun

Copy link
Copy Markdown
Contributor Author

Closing for now to land #247 first and break these changes into smaller, focused PRs aligned with DESIGN.md.

@tisonkun tisonkun closed this Nov 25, 2026
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