tracing-unwrap

This crate provides .unwrap_or_log() and .expect_or_log() methods on Result and Option types that log failed unwraps to a [tracing::Subscriber]. This is useful when, for example, you are logging to syslog or a database, and you want your unwrap failures to show up there instead of being printed to stderr.

Its API aims to mirror Rust's std — see all the supported methods below. Failed unwraps are logged at a level of [ERROR].

crates.io Documentation License

Usage

Add the following to your Cargo.toml: toml tracing-unwrap = "0.10"

Next, bring the [ResultExt] and/or [OptionExt] traits into scope, and make use of the new logging methods. ```rust use tracing_unwrap::ResultExt;

tracingsubscriber::fmt().init(); let notgreat: Result<(), _> = Result::Err("not terrible");

// Logs the failed unwrap and panics notgreat.unwrapor_log(); ```

Methods

| std method | tracing-unwrap form | trait | |--------------------------------| ----------------------------------------|---------------| | [Result::unwrap()] | [Result::unwrap_or_log()] | [ResultExt] | | [Result::expect(msg)] | [Result::expect_or_log(msg)] | [ResultExt] | | [Result::unwrap_err()] | [Result::unwrap_err_or_log()] | [ResultExt] | | [Result::expect_err(msg)] | [Result::expect_err_or_log(msg)] | [ResultExt] | | [Option::unwrap()] | [Option::unwrap_or_log()] | [OptionExt] | | [Option::expect(msg)] | [Option::expect_or_log(msg)] | [OptionExt] | | [Option::unwrap_none()] | [Option::unwrap_none_or_log()] | [OptionExt] | | [Option::expect_none(msg)] | [Option::expect_none_or_log(msg)] | [OptionExt] |

†: no longer in std, see rust-lang/rust#62633

Features