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

Ignore errors from env_logger init. #2170

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

EnigmaCurry
Copy link
Contributor

I want to use just as a rust library from my existing rust application, which already uses env_logger:

use env_logger::Env;

fn main() {
    env_logger::init_from_env(
        Env::default()
            .filter_or("LOG_LEVEL", "warn")
            .write_style_or("LOG_STYLE", "always"),
    );
    set_env_default("DATABASE_URL");

    // This fails with env_logger double initialization error:
    if let Err(code) = just::run() {
        std::process::exit(code);
    }
}

If I run just::run() after I have already initialized env_logger, I get this error:

Builder::init should not be called after logger initialized: SetLoggerError(())
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It looks like the correct fix is to use env_logger's try_init instead, and so that's what I've done, which fixes the above code to have no more errors.

@casey casey enabled auto-merge (squash) June 19, 2024 07:48
@casey casey merged commit fcac7ee into casey:master Jun 19, 2024
5 checks passed
@casey
Copy link
Owner

casey commented Jun 19, 2024

Seems reasonable to me!

@casey
Copy link
Owner

casey commented Jun 19, 2024

Also, although just isn't really designed to be used as a library, I'm not opposed to doing simple things to make using it as a library easier, like making run() take command line arguments, so they can be passed from a source other than the binary's command line arguments.

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