From 119a3cbfcd9a87c8a9910314146563048c90b198 Mon Sep 17 00:00:00 2001 From: andrussal Date: Wed, 10 Dec 2025 13:42:09 +0100 Subject: [PATCH] Share logging setup helper for node spawns --- .../core/src/nodes/common/lifecycle/spawn.rs | 26 +++++++++++++++++-- testing-framework/core/src/nodes/executor.rs | 21 +++------------ testing-framework/core/src/nodes/validator.rs | 21 +++------------ 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/testing-framework/core/src/nodes/common/lifecycle/spawn.rs b/testing-framework/core/src/nodes/common/lifecycle/spawn.rs index 518ab8b..3d785cf 100644 --- a/testing-framework/core/src/nodes/common/lifecycle/spawn.rs +++ b/testing-framework/core/src/nodes/common/lifecycle/spawn.rs @@ -1,4 +1,26 @@ #![allow(dead_code)] -/// Shared spawn helpers (placeholder). -pub struct SpawnManager; +use std::path::Path; + +use nomos_tracing::logging::local::FileConfig; + +/// Configure tracing logger to write into `NOMOS_LOG_DIR` if set, else into the +/// provided base dir. +pub fn configure_logging(base_dir: &Path, prefix: &str, set_logger: F) +where + F: FnOnce(FileConfig), +{ + if let Ok(env_dir) = std::env::var("NOMOS_LOG_DIR") { + let log_dir = std::path::PathBuf::from(env_dir); + let _ = std::fs::create_dir_all(&log_dir); + set_logger(FileConfig { + directory: log_dir, + prefix: Some(prefix.into()), + }); + } else { + set_logger(FileConfig { + directory: base_dir.to_owned(), + prefix: Some(prefix.into()), + }); + } +} diff --git a/testing-framework/core/src/nodes/executor.rs b/testing-framework/core/src/nodes/executor.rs index bba602b..5cf3f19 100644 --- a/testing-framework/core/src/nodes/executor.rs +++ b/testing-framework/core/src/nodes/executor.rs @@ -18,7 +18,6 @@ use nomos_executor::config::Config; use nomos_http_api_common::paths::{DA_GET_SHARES_COMMITMENTS, MANTLE_METRICS, MEMPOOL_ADD_TX}; use nomos_network::backends::libp2p::Libp2pInfo; use nomos_node::api::testing::handlers::HistoricSamplingRequest; -use nomos_tracing::logging::local::FileConfig; use nomos_tracing_service::LoggerLayer; use reqwest::Url; pub use testing_framework_config::nodes::executor::create_executor_config; @@ -31,7 +30,7 @@ use crate::{ common::{ binary::{BinaryConfig, BinaryResolver}, config::{injection::inject_ibd_into_cryptarchia, paths::ensure_recovery_paths}, - lifecycle::kill::kill_child, + lifecycle::{kill::kill_child, spawn::configure_logging}, }, }, }; @@ -78,21 +77,9 @@ impl Executor { let _ = ensure_recovery_paths(dir.path()); if !*IS_DEBUG_TRACING { - if let Ok(env_dir) = std::env::var("NOMOS_LOG_DIR") { - let log_dir = PathBuf::from(env_dir); - let _ = std::fs::create_dir_all(&log_dir); - config.tracing.logger = LoggerLayer::File(FileConfig { - directory: log_dir, - prefix: Some(LOGS_PREFIX.into()), - }); - } else { - // If no explicit log dir is provided, fall back to a tempdir so we can capture - // logs. - config.tracing.logger = LoggerLayer::File(FileConfig { - directory: dir.path().to_owned(), - prefix: Some(LOGS_PREFIX.into()), - }); - } + configure_logging(dir.path(), LOGS_PREFIX, |cfg| { + config.tracing.logger = LoggerLayer::File(cfg); + }); } config.storage.db_path = dir.path().join("db"); diff --git a/testing-framework/core/src/nodes/validator.rs b/testing-framework/core/src/nodes/validator.rs index 0065da2..1082c60 100644 --- a/testing-framework/core/src/nodes/validator.rs +++ b/testing-framework/core/src/nodes/validator.rs @@ -15,7 +15,6 @@ use nomos_da_network_service::MembershipResponse; use nomos_http_api_common::paths::{CRYPTARCHIA_HEADERS, DA_GET_SHARES_COMMITMENTS}; use nomos_network::backends::libp2p::Libp2pInfo; use nomos_node::{Config, HeaderId, api::testing::handlers::HistoricSamplingRequest}; -use nomos_tracing::logging::local::FileConfig; use nomos_tracing_service::LoggerLayer; use reqwest::Url; pub use testing_framework_config::nodes::validator::create_validator_config; @@ -30,7 +29,7 @@ use crate::{ common::{ binary::{BinaryConfig, BinaryResolver}, config::{injection::inject_ibd_into_cryptarchia, paths::ensure_recovery_paths}, - lifecycle::kill::kill_child, + lifecycle::{kill::kill_child, spawn::configure_logging}, }, }, }; @@ -102,21 +101,9 @@ impl Validator { let _ = ensure_recovery_paths(dir.path()); if !*IS_DEBUG_TRACING { - if let Ok(env_dir) = std::env::var("NOMOS_LOG_DIR") { - let log_dir = PathBuf::from(env_dir); - let _ = std::fs::create_dir_all(&log_dir); - config.tracing.logger = LoggerLayer::File(FileConfig { - directory: log_dir, - prefix: Some(LOGS_PREFIX.into()), - }); - } else { - // If no explicit log dir is provided, fall back to a tempdir so we can capture - // logs. - config.tracing.logger = LoggerLayer::File(FileConfig { - directory: dir.path().to_owned(), - prefix: Some(LOGS_PREFIX.into()), - }); - } + configure_logging(dir.path(), LOGS_PREFIX, |cfg| { + config.tracing.logger = LoggerLayer::File(cfg); + }); } config.storage.db_path = dir.path().join("db");