use std::{env, sync::LazyLock};
static CONSOLE_ENABLED: LazyLock<i8> = LazyLock::new(|| {
let var_level = env::var("J4RS_CONSOLE_LOG_LEVEL")
.unwrap_or("warn".to_owned())
.to_lowercase();
match var_level.as_str() {
"disabled" => 0,
"error" => 1,
"warn" => 2,
"info" => 3,
"debug" => 4,
_ => {
println!("WARN: The env variable 'J4RS_CONSOLE_LOG_LEVEL' is not correctly set. Please use one of the 'debug', 'info', 'warn', 'error', or 'disabled'. Defaulting to warning");
2
}
}
});
pub fn debug(message: &str) {
if is_console_debug_enabled() {
println!("DEBUG: {}", message);
}
debug!("{}", message);
}
pub fn info(message: &str) {
if is_console_info_enabled() {
println!("INFO: {}", message);
}
info!("{}", message);
}
#[allow(dead_code)]
pub fn warn(message: &str) {
if is_console_warn_enabled() {
println!("WARN: {}", message);
}
warn!("{}", message);
}
#[allow(dead_code)]
pub fn error(message: &str) {
if is_console_error_enabled() {
println!("ERROR: {}", message);
}
error!("{}", message);
}
pub fn is_console_debug_enabled() -> bool {
return CONSOLE_ENABLED.to_owned() > 3;
}
pub fn is_console_info_enabled() -> bool {
return CONSOLE_ENABLED.to_owned() > 2;
}
pub fn is_console_warn_enabled() -> bool {
return CONSOLE_ENABLED.to_owned() > 1;
}
pub fn is_console_error_enabled() -> bool {
return CONSOLE_ENABLED.to_owned() > 0;
}