rust-cli / rust-cli/env_logger

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

Open
#259 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.1k
Forks
149
Avg merge
2h 42m
Merged PRs (30d)
4

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`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the logger builder behavior around filter_level(level), filter(None, level), and RUST_LOG initialization. Determine how their priorities are currently applied, then define the expected precedence so RUST_LOG can override the hardcoded level and add coverage for both initialization forms.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
observability-sre
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.