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

Using with envfilter skips some logs #3214

Open
Crispy13 opened this issue Feb 8, 2025 · 0 comments
Open

Using with envfilter skips some logs #3214

Crispy13 opened this issue Feb 8, 2025 · 0 comments

Comments

@Crispy13
Copy link

Crispy13 commented Feb 8, 2025

Bug Report

Version

tracing = "0.1.41"
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3.19", features = ["chrono", "env-filter"] }

Platform

4.18.0-305.3.1.el8.x86_64 #1 SMP Tue Jun 1 16:14:33 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
CentOS

Crates

  • tracing
  • tracing-subscriber

Description

I am experiencing an issue where some logs do not appear when using my_crate=debug in the RUST_LOG environment variable. This problem occurs in the actual code, but the logs do appear when I use the same logging setup in a test function.

Steps to Reproduce:

  1. Set the RUST_LOG environment variable to my_crate=debug.
  2. Log some messages using event!(Level::DEBUG, "debug message").
  3. The logs do not appear when running the main application, but they show up correctly in the test function with the same logging setup.
pub fn setup_logging_to_stderr_and_rolling_file(
    filename_prefix: &str,
    // stderr_log_level: filter::LevelFilter,
) -> Result<(), Error> {
    let stderr_log_level = filter::LevelFilter::INFO;
    let stderr_layer = tracing_subscriber::fmt::layer()
        .pretty()
        .with_writer(io::stderr);

    let tmp_dir = get_tmp_dir();

    let file_layer = tracing_subscriber::fmt::layer().pretty().with_writer(
        RollingFileAppender::builder()
            .rotation(Rotation::DAILY)
            .filename_prefix(filename_prefix)
            .filename_suffix("log")
            .build(&tmp_dir)?,
    );

    tracing_subscriber::registry()
        .with(
            stderr_layer
                .with_timer(ChronoLocal::rfc_3339())
                .with_file(false)
                .with_line_number(false)
                .with_target(false)
                .with_filter(get_env_filter(stderr_log_level)?),
        )
        .with(
            file_layer
                .with_timer(ChronoLocal::rfc_3339())
                .with_ansi(false)
                .with_filter(get_env_filter(filter::LevelFilter::DEBUG)?),
        )
        .try_init()?;

    let log_dir_abs_path = match Path::new(&tmp_dir).canonicalize() {
        Ok(v) => v,
        Err(_) => PathBuf::from(tmp_dir),
    };

    // event!(Level::INFO, "log dir = {}", log_dir_abs_path.display());

    Ok(())
}

#[tokio::main]
fn main() -> Result<(), Error> {
    setup_logging_to_stderr_and_rolling_file("log")?;

    event!(Level::DEBUG, "This is a debug message");

    // Simulate some other work
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;

    event!(Level::WARN, "This is a warning message");
}

Expected Behavior:

All logs at the DEBUG level (and higher) should be displayed when my_crate=debug is set in the RUST_LOG environment variable.

Actual Behavior:

Logs at the DEBUG level are missing when my_crate=debug is set, and only higher-level logs (e.g., WARN, ERROR) appear. However, the same setup works fine in the test function.

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

1 participant