6 个版本 (1 个稳定版)
| 1.0.1 | 2024 年 3 月 12 日 |
|---|---|
| 0.10.0 | 2022 年 11 月 4 日 |
| 0.9.2 | 2020 年 8 月 27 日 |
| 0.0.1 | 2020 年 8 月 9 日 |
在 调试 中排名 79
每月下载量 6,419
在 11 个 Crates 中使用(8 直接使用)
21KB
193 代码行
tracing-unwrap
此 crate 为 .unwrap_or_log() 和 .expect_or_log() 方法提供了 Result 和 Option 类型,这些方法将失败的 unwraps 日志记录到 tracing::Subscriber。当您例如向 syslog 或数据库记录日志时,这将很有用,您希望 unwrap 失败显示在那里,而不是打印到 stderr。
其 API 旨在与 Rust 的 std 相似 —— 请参阅以下所有支持的 方法。失败的 unwraps 以 ERROR 级别进行日志记录。
用法
将以下内容添加到您的 Cargo.toml
tracing-unwrap = "1.0"
接下来,将 ResultExt 和/或 OptionExt traits 带入作用域,并使用新日志方法。
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();
方法
_†: 已不再在 std 中,请参阅 rust-lang/rust#62633_
功能
-
panic-quiet:导致失败的解包抛出没有消息的恐慌。
该功能默认启用 — 如果您希望解包错误消息也显示在恐慌消息中,请按如下方式在您的Cargo.toml中禁用默认功能
tracing-unwrap= {version= "1.0",default-features= false } -
log-location:调用std::panic::Location::caller()来确定失败的解包位置。
依赖关系
~420KB