Macro try_init_cli_log

Source
macro_rules! try_init_cli_log {
    () => { ... };
    ($app_name: expr) => { ... };
}
Expand description

Configure the application log according to env variable.

If an io error occurs, it is returned, and all logging is disabled (but there won’t be any panic or error on log calls).

The caller can decide to print the error and not, to continue or not.

Example:

cli_log::try_init_cli_log!().expect("Failed to initialize log");

You may specify an altername application name instead of your crate name:

if Err(e) = cli_log::try_init_cli_log!("my-app") {
     eprintln!("Running without log because of error: {}", e);
}

The application name will also be used to derive the env variable name giving the log level, for example MY_APP_LOG=info for an application named my-app.

The point of using this macro instead of the init function is to ensure env!(CARGO_PKG_NAME) and env!(CARGO_PKG_VERSION) are expanded for the outer package, not for cli-log