Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Priority issues between environment variable RUST_LOG and function call filter_level / filter. #259

Open
VergeDX opened this issue Jan 31, 2023 · 1 comment

Comments

@VergeDX
Copy link

VergeDX commented Jan 31, 2023

Environment variable RUST_LOG can only used to set "default policy", which means if filter_level(level) or filter(None, level) (they are practically equivalent) called at logger init, then environment variable RUST_LOG will cannot override the hardcoded log level.

This because environment variable RUST_LOG will initialized first (by default), then override by program logic. This might be a bug because commonly priority of environment variable > cli flag > config file or hardcoded.

Demo:

use log::{info, LevelFilter};

fn main() {
    // Hardcoded log level by using `filter_level` or `filter`.
    env_logger::builder().filter_level(LevelFilter::Info).init();

    info!("starting up");
}

If set the environment variable RUST_LOG=warn, info output will still print:

demo on  master [?] is 📦 v0.1.0 via 🦀 v1.67.0
at 14:04:38 fsh ❯ RUST_LOG=warn cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/demo`
[2023-01-31T06:04:43Z INFO  demo] starting up

But if not setting log level at logger init:

use log::info;

fn main() {
    env_logger::builder().init();
    info!("starting up");
}

Then log level can be controlled easylly from RUST_LOG:

demo on  master [?] is 📦 v0.1.0 via 🦀 v1.67.0
at 14:07:17 fsh ❯ RUST_LOG=info cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/demo`
[2023-01-31T06:07:21Z INFO  demo] starting up

demo on  master [?] is 📦 v0.1.0 via 🦀 v1.67.0
at 14:07:21 fsh ❯ RUST_LOG=warn cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/demo`

@Skgland
Copy link

Skgland commented Jan 29, 2024

What about env_logger::Builder::default().filter_level(LevelFilter::Info).parse_default_env().init(); ?
By parsing the env after applying the filters the env should be able to overwrite them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants