Skip to content

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

Open
@VergeDX

Description

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`

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions