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`
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.
Environment variable
RUST_LOG
can only used to set "default policy", which means iffilter_level(level)
orfilter(None, level)
(they are practically equivalent) called at logger init, then environment variableRUST_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:
If set the environment variable
RUST_LOG=warn
, info output will still print:But if not setting log level at logger init:
Then log level can be controlled easylly from
RUST_LOG
: