use crate::config::{LogConfig, TelemetryConfig};
#[allow(dead_code)]
pub fn init(config: &LogConfig) {
let profile = crate::config::resolve_profile(&crate::config::OsEnv);
let _ = init_with_telemetry(config, &TelemetryConfig::default(), profile.as_deref())
.unwrap_or_else(|error| panic!("failed to initialize logging: {error}"));
}
pub fn init_with_telemetry(
config: &LogConfig,
telemetry: &TelemetryConfig,
profile: Option<&str>,
) -> Result<crate::telemetry::TelemetryGuard, crate::telemetry::TelemetryInitError> {
crate::telemetry::init(config, telemetry, profile)
}
#[cfg(test)]
fn is_production() -> bool {
std::env::var("AUTUMN_ENV").is_ok_and(|v| v.eq_ignore_ascii_case("production"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{LogConfig, LogFormat};
#[test]
fn is_production_false_by_default() {
assert!(!is_production());
}
#[test]
fn auto_format_is_not_json_in_non_production() {
let use_json = match LogFormat::Auto {
LogFormat::Auto => is_production(),
LogFormat::Pretty => false,
LogFormat::Json => true,
};
assert!(!use_json);
}
#[test]
fn pretty_format_is_never_json() {
let use_json = match LogFormat::Pretty {
LogFormat::Auto => is_production(),
LogFormat::Pretty => false,
LogFormat::Json => true,
};
assert!(!use_json);
}
#[test]
fn json_format_is_always_json() {
let use_json = match LogFormat::Json {
LogFormat::Auto => is_production(),
LogFormat::Pretty => false,
LogFormat::Json => true,
};
assert!(use_json);
}
#[test]
fn valid_filter_parses() {
let config = LogConfig {
level: "debug".to_owned(),
format: LogFormat::Auto,
};
let filter = tracing_subscriber::EnvFilter::try_new(&config.level);
assert!(filter.is_ok());
}
#[test]
fn invalid_filter_falls_back() {
let filter = tracing_subscriber::EnvFilter::try_new("not_a_valid_[directive");
assert!(filter.is_err());
}
}