gpt4 book ai didi

logging - 在 Rust 中删除调试宏

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

我正在编写一个应用程序,它在执行期间使用 slog 记录多项内容。因此,我广泛使用了 info!error!warn!debug! 宏。

但是,正如预期的那样,debug! 调用可以帮助我调试应用程序,我不希望这些调用在实际使用应用程序时污染日志。

我一直在尝试编译它们,但没有成功。这是我正在使用的行:RUSTFLAGS="$RUSTFLAGS -C debug-assertions"cargo build --release

编译进行得很顺利,但在执行时我看到了所有的调试调用。

以下是我的问题的一个工作示例:

cargo .toml:

[dependencies]
slog = { version = "1.5", features = ["max_level_trace", "release_max_level_warn"] }
slog-stream = "1.2.0"
slog-term = "1.5.0"
slog-json = "1.2.1"
slog-stdlog = "1.1.0"
log = "0.3.7"

主要.rs:

#[macro_use]
extern crate slog;
extern crate slog_stream;
extern crate slog_term;
extern crate slog_json;
extern crate slog_stdlog;
#[macro_use]
extern crate log;

use std::path::Path;
use std::fs::OpenOptions;
use slog::DrainExt;

fn init_logger(work_dir : &Path) {
let mut log_dir_buf = work_dir.to_path_buf();
log_dir_buf.push("log");

if !log_dir_buf.exists() {
std::fs::create_dir(log_dir_buf.as_path()).unwrap();
}

log_dir_buf.push("the_log_file.log");
let log_file_name = log_dir_buf.to_str().unwrap();

let log_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log_file_name).unwrap();

let console_drain = slog_term::streamer().build();
let file_drain = slog_stream::stream(log_file, slog_json::default());
let logger = slog::Logger::root(slog::duplicate(console_drain, file_drain).fuse(), o!());
slog_stdlog::set_logger(logger).unwrap();
}

fn main() {
init_logger(Path::new("."));

info!("This is an info message");
warn!("This is a warn message");
error!("This is an error message");
debug!("This is a debug message");
}

现在,如上所述构建发布版本并运行二进制文件,我得到以下输出:

Jan 23 17:20:56.640 INFO This is an info message
Jan 23 17:20:56.641 WARN This is a warn message
Jan 23 17:20:56.641 ERRO This is an error message
Jan 23 17:20:56.641 DEBG This is a debug message

最后,rust 版本:rustc 1.31.0 (abe02cefd 2018-12-04)

我知道我使用的 slog 版本很旧,但目前升级依赖不是当务之急。然而,version's documentation指出日志过滤应该是可能的,正如@shepmaster 在 linked answer 中描述的那样,它似乎对我不起作用。我错过了什么?

最佳答案

为了简化问题,我使用当前版本的 slog (2.4.1) 制作了另一个工作示例,它设置了一个全局范围的记录器,问题仍然重现。这是代码示例:

extern crate slog;
extern crate slog_async;
extern crate slog_json;
extern crate slog_term;
extern crate slog_scope;
extern crate slog_stdlog;
#[macro_use]
extern crate log;

fn complex_logging() -> Result<(), log::SetLoggerError> {
//Create the output file
let log_path = "your_log_file_path.log";
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(log_path)
.unwrap();

//Create the terminal drain
let decorator = slog_term::TermDecorator::new().build();
let d1 = slog_term::FullFormat::new(decorator).build().fuse();
let d1 = slog_async::Async::new(d1).build().fuse();

//Create the file drain
let d2 = slog_json::Json::new(file)
.add_default_keys()
.build()
.fuse();
let d2 = slog_async::Async::new(d2).build().fuse();

//Fuse the drains and create the logger
let logger = slog::Logger::root(slog::Duplicate::new(d1, d2).fuse(), o!());
let _guard = slog_scope::set_global_logger(logger);

//register slog_stdlog as the log handler with the log crate
slog_stdlog::init().unwrap();


trace!("logging a trace message");
debug!("debug values \"x\" => 1, \"y\" => -1");
info!("some interesting info; where => right here");
warn!("be cautious!; why => you never know...");
error!("wrong, foobar; type => unknown");

Ok(())
}

fn main() {
let _res = complex_logging();
}

在构建发布后运行上面的示例会产生以下结果:

Jan 30 13:53:52.398 TRCE logging a trace message
Jan 30 13:53:52.399 DEBG debug values "x" => 1, "y" => -1
Jan 30 13:53:52.399 INFO some interesting info; where => right here
Jan 30 13:53:52.399 WARN be cautious!; why => you never know...
Jan 30 13:53:52.399 ERRO wrong, foobar; type => unknown

但是,一个传递记录器的新示例解决了这个问题。这是工作示例:

#[macro_use]
extern crate slog;
extern crate slog_async;
extern crate slog_json;
extern crate slog_term;
extern crate slog_scope;
extern crate slog_stdlog;

use slog::Drain;
use std::fs::OpenOptions;
use std::io;
use std::sync::Mutex;

fn duplicate_log() {
let plain = slog_term::PlainSyncDecorator::new(std::io::stdout());
let d1 = slog_term::FullFormat::new(plain).build().fuse();
let d2 = Mutex::new(slog_json::Json::default(io::stdout())).fuse();
let log = slog::Logger::root(slog::Duplicate::new(d1, d2).fuse(), o!("version" => env!("CARGO_PKG_VERSION")));

trace!(log, "logging a trace message");
debug!(log, "debug values"; "x" => 1, "y" => -1);
info!(log, "some interesting info"; "where" => "right here");
warn!(log, "be cautious!"; "why" => "you never know...");
error!(log, "wrong {}", "foobar"; "type" => "unknown");
crit!(log, "abandoning test");
}

fn main() {
duplicate_log();
}

运行工作示例会产生以下结果:

Jan 30 13:56:30.839 INFO some interesting info, where: right here, version: 0.1.0
{"msg":"some interesting info","level":"INFO","ts":"2019-01-30T13:56:30.839762+00:00","version":"0.1.0","where":"right here"}
Jan 30 13:56:30.839 WARN be cautious!, why: you never know..., version: 0.1.0
{"msg":"be cautious!","level":"WARN","ts":"2019-01-30T13:56:30.839787+00:00","version":"0.1.0","why":"you never know..."}
Jan 30 13:56:30.839 ERRO wrong foobar, type: unknown, version: 0.1.0
{"msg":"wrong foobar","level":"ERRO","ts":"2019-01-30T13:56:30.839802+00:00","version":"0.1.0","type":"unknown"}
Jan 30 13:56:30.839 CRIT abandoning test, version: 0.1.0
{"msg":"abandoning test","level":"CRIT","ts":"2019-01-30T13:56:30.839815+00:00","version":"0.1.0"}

如果您遇到与我类似的问题,我的问题是设置全局范围记录器。传递记录器解决了这个问题。

关于logging - 在 Rust 中删除调试宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54330249/

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