diff --git a/src/cli.rs b/src/cli.rs index 7cb613b5..a179bc7c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -495,6 +495,12 @@ pub async fn playground() -> crate::Result { let cli = Playground::parse(); let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into(); + let config = environment.load()?; + + if !H::init_logger(&config, &environment)? { + logger::init::(&config.logger)?; + } + let app_context = create_context::(&environment).await?; Ok(app_context) } @@ -536,7 +542,7 @@ pub async fn main() -> crate::Result<()> { let config = environment.load()?; if !H::init_logger(&config, &environment)? { - logger::init::(&config.logger); + logger::init::(&config.logger)?; } let task_span = create_root_span(&environment); @@ -682,7 +688,7 @@ pub async fn main() -> crate::Result<()> { let config = environment.load()?; if !H::init_logger(&config, &environment)? { - logger::init::(&config.logger); + logger::init::(&config.logger)?; } let task_span = create_root_span(&environment); diff --git a/src/lib.rs b/src/lib.rs index 5cbd789f..fb29c0c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ mod env_vars; pub mod environment; pub mod errors; pub mod hash; -mod logger; +pub mod logger; pub mod mailer; pub mod scheduler; pub mod task; diff --git a/src/logger.rs b/src/logger.rs index cd791836..8bcd53c0 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -9,7 +9,7 @@ use tracing_subscriber::{ fmt, fmt::MakeWriter, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, Registry, }; -use crate::{app::Hooks, config}; +use crate::{app::Hooks, config, Error, Result}; // Define an enumeration for log levels #[derive(Debug, Default, Clone, Deserialize, Serialize)] @@ -75,6 +75,7 @@ const MODULE_WHITELIST: &[&str] = &[ "tower_http", "sqlx::query", "sidekiq", + "playground", ]; // Keep nonblocking file appender work guard @@ -96,7 +97,11 @@ static NONBLOCKING_WORK_GUARD_KEEP: OnceLock = OnceLock::new(); /// use via PR) /// 3. regardless of (1) and (2) operators in production, or elsewhere can /// always use `RUST_LOG` to quickly diagnose a service -pub fn init(config: &config::Logger) { +/// +/// # Errors +/// Fails if cannot initialize logger or set up an appender (in case the option +/// is enabled) +pub fn init(config: &config::Logger) -> Result<()> { let mut layers: Vec + Sync + Send>> = Vec::new(); if let Some(file_appender_config) = config.file_appender.as_ref() { @@ -138,12 +143,14 @@ pub fn init(config: &config::Logger) { .map_or_else(String::new, ToString::to_string), ) .build(dir) - .expect("logger file appender initialization failed"); + .map_err(Error::msg)?; let file_appender_layer = if file_appender_config.non_blocking { let (non_blocking_file_appender, work_guard) = tracing_appender::non_blocking(file_appender); - NONBLOCKING_WORK_GUARD_KEEP.set(work_guard).unwrap(); + NONBLOCKING_WORK_GUARD_KEEP + .set(work_guard) + .map_err(|_| Error::string("cannot lock for appender"))?; init_layer( non_blocking_file_appender, &file_appender_config.format, @@ -168,6 +175,7 @@ pub fn init(config: &config::Logger) { .with(env_filter) .init(); } + Ok(()) } fn init_env_filter(override_filter: Option<&String>, level: &LogLevel) -> EnvFilter {