| Crates.io | slog-unwrap |
| lib.rs | slog-unwrap |
| version | 0.9.2 |
| created_at | 2020-04-05 16:19:17.148643+00 |
| updated_at | 2020-08-27 18:35:58.824462+00 |
| description | Extension traits for logging failed unwraps to a slog::Logger. |
| homepage | |
| repository | https://github.com/abreis/slog-unwrap |
| max_upload_size | |
| id | 226617 |
| size | 36,004 |
This crate provides .unwrap_or_log() and .expect_or_log() methods on Result and Option types that log failed unwraps to a slog::Logger. This is useful when, for example, you have a syslog drain or a database drain, 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 Critical.
Add the following to your Cargo.toml:
slog-unwrap = "0.9"
Next, bring the ResultExt and/or OptionExt traits into scope, and make use of the new logging methods.
use slog_unwrap::ResultExt;
let logger = slog::Logger::root(slog::Discard, slog::o!());
let not_great: Result<(), _> = Result::Err("not terrible");
// Logs the failed unwrap to `logger` and panics
not_great.unwrap_or_log(&logger);
†: unstable in std
Note: enabling the scope feature drops the &log argument from all methods.
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:
slog-unwrap = { version = "0.9", default-features = false }
scope: adds support for slog-scope, which removes the need to pass a slog::Logger to the various methods.
See slog-unwraps, another crate with a similar featureset.