use tracing_subscriber::{EnvFilter, Layer, Registry, fmt};
use crate::config::{LogConfig, LogFormat};
pub(crate) type BoxedLayer = Box<dyn Layer<Registry> + Send + Sync + 'static>;
pub(crate) fn env_filter(config: &LogConfig) -> EnvFilter {
EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new(&config.level))
.unwrap_or_else(|_| EnvFilter::new("info"))
}
pub(crate) fn fmt_layer(config: &LogConfig) -> BoxedLayer {
match config.format {
LogFormat::Pretty => fmt::layer().pretty().with_ansi(config.ansi).boxed(),
LogFormat::Compact => fmt::layer().compact().with_ansi(config.ansi).boxed(),
LogFormat::Json => fmt::layer().json().with_current_span(true).flatten_event(true).boxed(),
}
}