无法在模块上控制env_logger VIS RUST_LOG
我遇到了一个问题,即将log/env_logger纳入我的代码。
我已经记录了未释放的记录,如果我在全球级别启用内容(rust_log = debug)或在应用程序级别(rust_log = testlogapp),它似乎可以正常工作)似乎不起作用。
cargo.toml :
[package]
name = "test_log_app"
version = "0.1.0"
edition = "2021"
[dependencies]
env_logger = "0.9.0"
log = "0.4.16"
src/main.rs
#[macro_use] extern crate log;
fn main() {
env_logger::init();
debug!("this is a debug {}", "message");
error!("this is printed by default");
let x = 3 * 4;
info!("the answer was: {}", x);
}
我得到的结果是:
rust_log = debug - 工作
$ RUST_LOG=debug cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
[2022-06-22T16:36:40Z DEBUG test_log_app] this is a debug message
[2022-06-22T16:36:40Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:40Z INFO test_log_app] the answer was: 12
rust_log = info -
$ RUST_LOG=info cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
[2022-06-22T16:36:47Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:47Z INFO test_log_app] the answer was: 12
按应用程序名称进行作品 rust_log = test_log_app -
$ RUST_LOG=test_log_app cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/test_log_app`
[2022-06-22T16:36:55Z DEBUG test_log_app] this is a debug message
[2022-06-22T16:36:55Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:55Z INFO test_log_app] the answer was: 12
模块的作品 rust_log = main - 失败,而没有记录
$ RUST_LOG=main cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
输出我期望rust_log = main的文档也应启用记录... 我是想念什么还是做错了什么? 非常感谢
I'm having an issue where I'm incorporating log/env_logger in my code.
I've loggging incoroprated and it appears to be working correctly if I enable things at a global level (RUST_LOG=debug) or at a application level (RUST_LOG=testlogapp) but if I try to enable logging at a module level (RUST_LOG=main) it doesn't seem to work.
Cargo.toml:
[package]
name = "test_log_app"
version = "0.1.0"
edition = "2021"
[dependencies]
env_logger = "0.9.0"
log = "0.4.16"
src/main.rs
#[macro_use] extern crate log;
fn main() {
env_logger::init();
debug!("this is a debug {}", "message");
error!("this is printed by default");
let x = 3 * 4;
info!("the answer was: {}", x);
}
The results I get are ase follows:
RUST_LOG=debug -- WORKS
$ RUST_LOG=debug cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
[2022-06-22T16:36:40Z DEBUG test_log_app] this is a debug message
[2022-06-22T16:36:40Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:40Z INFO test_log_app] the answer was: 12
RUST_LOG=info -- WORKS
$ RUST_LOG=info cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
[2022-06-22T16:36:47Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:47Z INFO test_log_app] the answer was: 12
by application name RUST_LOG=test_log_app -- WORKS
$ RUST_LOG=test_log_app cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/test_log_app`
[2022-06-22T16:36:55Z DEBUG test_log_app] this is a debug message
[2022-06-22T16:36:55Z ERROR test_log_app] this is printed by default
[2022-06-22T16:36:55Z INFO test_log_app] the answer was: 12
By Module RUST_LOG=main -- FAILS with no logging output
$ RUST_LOG=main cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/test_log_app`
From the docs I expect RUST_LOG=main should enable the logging as well...
Am I missing something or doing something incorrectly?
Much thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件
main.rs
的模块的名称是不是main
。这是板条箱的名称。证明:
::
The name of the module of the file
main.rs
is notmain
. It is the name of the crate.Proof:
Output in the playground: