Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to programmatically change the default logging level of the log crate?

I'm developing a project in Rust that is meant to be used by systems administrators, via CLI. In this program, I would like to have lines like these:

warn!("File {} not found, proceeding to next file", file_path);

Which I don't consider errors in the context of the software, but I still would want my users to be aware of.

However, Rust's logging system, by default, only prints messages on the ERROR log level, and the only way I found to change this default is to set the RUST_LOG environment variable - which I don't want my users to have to do. I guess I could create a wrapper script that just sets the variable and execs the program, but I would rather not.

Is there a way to change the default level programmatically, from inside the program?

like image 815
Renato Zannon Avatar asked Jan 20 '26 22:01

Renato Zannon


1 Answers

Here's a trick which will make it

if env::var("RUST_LOG").is_err() {
    env::set_var("RUST_LOG", "info")
}
env_logger::init();
like image 66
tworec Avatar answered Jan 23 '26 19:01

tworec