#log #logging #tracing

tracing-unwrap

Extension traits for logging failed unwraps to a tracing subscriber

4 releases

0.9.2 Aug 27, 2020
0.9.1 Aug 10, 2020
0.9.0 Aug 10, 2020
0.0.1 Aug 9, 2020

#172 in Debugging

Download history 189/week @ 2021-09-28 166/week @ 2021-10-05 345/week @ 2021-10-12 327/week @ 2021-10-19 302/week @ 2021-10-26 251/week @ 2021-11-02 429/week @ 2021-11-09 342/week @ 2021-11-16 180/week @ 2021-11-23 558/week @ 2021-11-30 275/week @ 2021-12-07 462/week @ 2021-12-14 509/week @ 2021-12-21 266/week @ 2021-12-28 469/week @ 2022-01-04 365/week @ 2022-01-11

1,686 downloads per month
Used in 4 crates (3 directly)

Apache-2.0/MIT

17KB
130 lines

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:

tracing-unwrap = "0.9"

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();

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

†: unstable in std

Features

  • 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 = "0.9", default-features = false }

Dependencies

~400KB