diff --git a/nomos-services/log/src/lib.rs b/nomos-services/log/src/lib.rs index afa726ce..8578e08a 100644 --- a/nomos-services/log/src/lib.rs +++ b/nomos-services/log/src/lib.rs @@ -144,6 +144,11 @@ macro_rules! registry_init { #[async_trait::async_trait] impl ServiceCore for Logger { fn init(service_state: ServiceStateHandle) -> Result { + #[cfg(test)] + use std::sync::Once; + #[cfg(test)] + static ONCE_INIT: Once = Once::new(); + let config = service_state.settings_reader.get_updated_settings(); let (non_blocking, _guard) = match config.backend { LoggerBackend::Gelf { addr } => { @@ -152,7 +157,13 @@ impl ServiceCore for Logger { .overwatch_handle .runtime() .spawn(async move { task.connect().await }); + #[cfg(test)] + ONCE_INIT.call_once(move || { + registry_init!(layer, config.format, config.level); + }); + #[cfg(not(test))] registry_init!(layer, config.format, config.level); + return Ok(Self { service_state, worker_guard: None, @@ -179,7 +190,13 @@ impl ServiceCore for Logger { let layer = tracing_subscriber::fmt::Layer::new() .with_level(true) .with_writer(non_blocking); + #[cfg(test)] + ONCE_INIT.call_once(move || { + registry_init!(layer, config.format, config.level); + }); + #[cfg(not(test))] registry_init!(layer, config.format, config.level); + Ok(Self { service_state, worker_guard: Some(_guard),