pub fn try_init_custom_env_and_builder(
log_env_var: &str,
global_log_env_var: &str,
package_name: &str,
module_name: &str,
builder_fn: impl Fn() -> Builder
) -> Result<(), SetLoggerError>
Expand description
Initializes the global logger with a pretty, sensible env logger, with custom variable names and a custom builder function.
This should be called early in the execution of a Rust program, and the global logger may only be initialized once. Future initialization attempts will return an error.
Example
sensible_env_logger::try_init_custom_env_and_builder(
"MY_RUST_LOG",
"MY_GLOBAL_RUST_LOG",
env!("CARGO_PKG_NAME"),
module_path!(),
sensible_env_logger::pretty::formatted_timed_builder,
);
How It works
The package_name
and module_name
arguments are ideally evaluated from
the $CARGO_PKG_NAME
and $CARGO_CRATE_NAME
environment variables
respectively. These environment variables are automatically set
by Cargo when compiling your crate. It then builds a custom directives
string in the same form as the $RUST_LOG
environment variable, and then
parses this generated directives string using
env_logger::Builder::parse_filters
.
Errors
This function fails to set the global logger if one has already been set.