无法在模块上控制env_logger VIS RUST_LOG

发布于 2025-02-10 04:44:05 字数 2096 浏览 0 评论 0原文

我遇到了一个问题,即将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2025-02-17 04:44:06

文件main.rs的模块的名称是不是 main。这是板条箱的名称。

证明:

use std::path::PathBuf;

use itertools::Itertools;
use regex::Regex;

fn main() {
    let cargo_toml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
    let cargo_toml = std::fs::read_to_string(cargo_toml_path).unwrap();
    let package_name_regex = Regex::new(r#"name = "(.*)""#).unwrap();
    let crate_name = package_name_regex
        .captures_iter(&cargo_toml)
        .exactly_one()
        .expect("no package name or more than one `name` key in Cargo.toml")
        .get(1)
        .unwrap()
        .as_str();

    println!("file       = {}", file!());
    println!("crate name = {}", crate_name);
    println!("module     = {}", module_path!());
}

::

file       = src/main.rs
crate name = playground
module     = playground

The name of the module of the file main.rs is not main. It is the name of the crate.

Proof:

use std::path::PathBuf;

use itertools::Itertools;
use regex::Regex;

fn main() {
    let cargo_toml_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
    let cargo_toml = std::fs::read_to_string(cargo_toml_path).unwrap();
    let package_name_regex = Regex::new(r#"name = "(.*)""#).unwrap();
    let crate_name = package_name_regex
        .captures_iter(&cargo_toml)
        .exactly_one()
        .expect("no package name or more than one `name` key in Cargo.toml")
        .get(1)
        .unwrap()
        .as_str();

    println!("file       = {}", file!());
    println!("crate name = {}", crate_name);
    println!("module     = {}", module_path!());
}

Output in the playground:

file       = src/main.rs
crate name = playground
module     = playground
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文