gpt4 book ai didi

debugging - 记录源文件和行号

转载 作者:行者123 更新时间:2023-12-03 11:26:34 28 4
gpt4 key购买 nike

使用 Rust 的 log , 和 env_logger crates,我如何让输出包括调用日志调用的源文件和行号?

最佳答案

在以下示例中 logger_example是我在 Cargo.toml 中的二进制文件的名称,例如

[[bin]]
name = "logger_example"
path = "src/bin/logger_example.rs"
您可以像这样自定义格式记录器:
use log::{info, LevelFilter};
use std::io::Write;

fn main() {
env_logger::Builder::new()
.format(|buf, record| {
writeln!(
buf,
"{}:{} {} [{}] - {}",
record.file().unwrap_or("unknown"),
record.line().unwrap_or(0),
chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"),
record.level(),
record.args()
)
})
.filter(Some("logger_example"), LevelFilter::Debug)
.init();

info!("hello world")
}

输出是:
src/bin/logger_example.rs:20 2020-11-30T19:34:16 [INFO] - hello world
我在这里找到了答案: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html

关于debugging - 记录源文件和行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61810740/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com