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

[Feature Request] Default log level #36

Open
pheki opened this issue Jun 5, 2020 · 7 comments
Open

[Feature Request] Default log level #36

pheki opened this issue Jun 5, 2020 · 7 comments

Comments

@pheki
Copy link

pheki commented Jun 5, 2020

In env_logger it's possible to provide a default log level by using:

env_logger::from_env(Env::default().default_filter_or("info")).init();

Would you consider adding this feature (or maybe accepting a PR)?

@JMLX42
Copy link

JMLX42 commented Jun 11, 2020

Here is how we do it as a workaround:

if let Err(_) = std::env::var("SMARTSHAPE_CLI_LOG") {
    std::env::set_var("SMARTSHAPE_CLI_LOG", "warn");
}

@pheki
Copy link
Author

pheki commented Jun 15, 2020

@promethe42 nice workaround!

The only caveat is that if set RUST_LOG to a specific module, the default log level is lost

(also clippy recommends .is_err() instead of let Err(_))

@RalfJung
Copy link

The only caveat is that if set RUST_LOG to a specific module, the default log level is lost

I think the env_logger snippet has the same issue. At least, the default_filter_or docs say

Use the default environment variable to read the filter from.

If the variable is not set, the default value will be used.

@pheki
Copy link
Author

pheki commented Jun 16, 2020

Hmmmmmmmmmmmm that's true, didn't realize that

@jpmckinney
Copy link

jpmckinney commented Nov 18, 2022

I don't need the env vars to work (I have a CLI with a repeatable verbose flag) so I'm doing something like:

use log::LevelFilter;

pretty_env_logger::formatted_builder().filter_level(LevelFilter::Warn).init();

@link2xt
Copy link

link2xt commented Dec 17, 2023

This should be upstreamed to https://github.com/rust-cli/env_logger

In deltachat/deltachat-core-rust#5112 I did:

    pretty_env_logger::formatted_timed_builder()
        .parse_default_env()
        .filter_module("deltachat_repl", log::LevelFilter::Info)
        .init();

This parses RUST_LOG, then unconditionally overwrites the level for deltachat_repl module. What is missing is doing this only if deltachat_repl module is not already set (by .parse_default_env()).

Upstream issue is at rust-cli/env_logger#162

@Skgland
Copy link

Skgland commented Jan 29, 2024

This should be upstreamed to https://github.com/rust-cli/env_logger

In deltachat/deltachat-core-rust#5112 I did:

    pretty_env_logger::formatted_timed_builder()
        .parse_default_env()
        .filter_module("deltachat_repl", log::LevelFilter::Info)
        .init();

This parses RUST_LOG, then unconditionally overwrites the level for deltachat_repl module. What is missing is doing this only if deltachat_repl module is not already set (by .parse_default_env()).

Upstream issue is at rust-cli/env_logger#162

Wouldn't this be fixed by swapping the filter_module and parse_default_env call changing it to

    pretty_env_logger::formatted_timed_builder()
        .filter_module("deltachat_repl", log::LevelFilter::Info)
        .parse_default_env()
        .init();

that way the env variable should be able to overwrite the default.

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

6 participants