Crates.io | tracing-unwrap |
lib.rs | tracing-unwrap |
version | 1.0.1 |
source | src |
created_at | 2020-08-09 19:34:57.274537 |
updated_at | 2024-03-12 23:29:58.41018 |
description | Extension traits for logging failed unwraps to a tracing subscriber. |
homepage | |
repository | https://github.com/abreis/tracing-unwrap |
max_upload_size | |
id | 274782 |
size | 33,048 |
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
.
Add the following to your Cargo.toml
:
tracing-unwrap = "1.0"
Next, bring the ResultExt
and/or OptionExt
traits into scope, and make use of the new logging methods.
use tracing_unwrap::ResultExt;
tracing_subscriber::fmt().init();
let not_great: Result<(), _> = Result::Err("not terrible");
// Logs the failed unwrap and panics
not_great.unwrap_or_log();
†: no longer in std
, see rust-lang/rust#62633
panic-quiet
: causes failed unwraps to panic with an empty message.
This feature is enabled by default — if you'd like the unwrap error message to also show in the panic message, disable default features in your Cargo.toml
as follows:
tracing-unwrap = { version = "1.0", default-features = false }
log-location
: calls std::panic::Location::caller()
to determine the location of a failed unwrap.