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

fix: Enable lowering log level below startup value #15

Merged

Conversation

sgasse
Copy link
Contributor

@sgasse sgasse commented Oct 2, 2024

Logger::parse_filters allows to change the log filter even after the logger has been initialized. However, it was not possible to see logs which were above the level set at startup. E.g. if the logger was initialized with debug, setting it to trace at runtime did not work since the filter of the global logging facade filtered out the trace logs.

This commit fixes the issue by calling log::set_max_level with the level from the updated log filter.

`Logger::parse_filters` allows to change the log filter even after the
logger has been initialized. However, it was not possible to see logs
which were above the level set at startup. E.g. if the logger was
initialized with `debug`, setting it to `trace` at runtime did not work
since the filter of the global logging facade filtered out the `trace`
logs.

This commit fixes the issue by calling `log::set_max_level` with the
level from the updated log filter.
@sgasse
Copy link
Contributor Author

sgasse commented Oct 2, 2024

I tested the fix on an android target with this small binary:

[package]
name = "logger_level"
version = "0.1.0"
edition = "2021"

[dependencies]
android-logd-logger = { path = "../android-logd-logger" }
log = "0.4.22"
use std::io::stdin;

use log::{debug, error, info, trace, warn};

fn main() {
    let mut logger = android_logd_logger::builder()
        .parse_filters("info")
        .prepend_module(true)
        .init();

    let mut log_filter = String::with_capacity(5);
    let mut counter = 0;

    println!("Enter a log level like trace, info etc.");

    loop {
        log_filter.clear();

        stdin().read_line(&mut log_filter).unwrap();
        logger.parse_filters(&log_filter);

        counter += 1;

        trace!("{counter}");
        debug!("{counter}");
        info!("{counter}");
        warn!("{counter}");
        error!("{counter}");
    }
}

The logger is initialized with the info level as max level. As expected, setting it to debug or trace does not lead to debug or trace logs without the fix: The filter in the logger is updated, but the logging facade still filters out everything above info.

With the fix, the debug and trace logs show up if the level is later set to trace although it was initialized with info.

Copy link
Owner

@flxo flxo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

@flxo flxo merged commit 98e2b24 into flxo:main Oct 8, 2024
8 checks passed
@sgasse sgasse deleted the sgasse/fix/parse_filters_below_startup_level branch October 8, 2024 14:25
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

Successfully merging this pull request may close these issues.

2 participants