diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 01215d4009..47c7763153 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -4,6 +4,7 @@ namespace ldk_node { dictionary Config { string storage_dir_path = "/tmp/ldk_node/"; + string? log_dir_path = null; Network network = "Bitcoin"; NetAddress? listening_address = null; u32 default_cltv_expiry_delta = 144; diff --git a/src/builder.rs b/src/builder.rs index dddc6d0643..3c02815bab 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -202,6 +202,12 @@ impl NodeBuilder { self } + /// Sets the log dir path if logs need to live separate from the storage directory path. + pub fn set_log_dir_path(&mut self, log_dir_path: String) -> &mut Self { + self.config.log_dir_path = Some(log_dir_path); + self + } + /// Sets the Bitcoin network used. pub fn set_network(&mut self, network: Network) -> &mut Self { self.config.network = network; @@ -329,6 +335,11 @@ impl ArcedNodeBuilder { self.inner.write().unwrap().set_storage_dir_path(storage_dir_path); } + /// Sets the log dir path if logs need to live separate from the storage directory path. + pub fn set_log_dir_path(&self, log_dir_path: String) { + self.inner.write().unwrap().set_log_dir_path(log_dir_path); + } + /// Sets the Bitcoin network used. pub fn set_network(&self, network: Network) { self.inner.write().unwrap().set_network(network); @@ -377,14 +388,14 @@ fn build_with_store_internal( let bdk_data_dir = format!("{}/bdk", config.storage_dir_path); fs::create_dir_all(bdk_data_dir.clone()).map_err(|_| BuildError::StoragePathAccessFailed)?; + let log_dir = match &config.log_dir_path { + Some(log_dir) => String::from(log_dir), + None => config.storage_dir_path.clone() + "/logs", + }; + // Initialize the Logger - let log_file_path = format!( - "{}/logs/ldk_node_{}.log", - config.storage_dir_path, - chrono::offset::Local::now().format("%Y_%m_%d") - ); let logger = Arc::new( - FilesystemLogger::new(log_file_path.clone(), config.log_level) + FilesystemLogger::new(log_dir, config.log_level) .map_err(|_| BuildError::LoggerSetupFailed)?, ); diff --git a/src/lib.rs b/src/lib.rs index 1fbd33f783..fa835e6272 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,21 +198,26 @@ const WALLET_KEYS_SEED_LEN: usize = 64; /// /// ### Defaults /// -/// | Parameter | Value | -/// |----------------------------------------|------------------| -/// | `storage_dir_path` | /tmp/ldk_node/ | -/// | `network` | Bitcoin | -/// | `listening_address` | None | -/// | `default_cltv_expiry_delta` | 144 | -/// | `onchain_wallet_sync_interval_secs` | 80 | -/// | `wallet_sync_interval_secs` | 30 | -/// | `fee_rate_cache_update_interval_secs` | 600 | -/// | `trusted_peers_0conf` | [] | -/// | `log_level` | Debug | +/// | Parameter | Value | +/// |----------------------------------------|--------------------| +/// | `storage_dir_path` | /tmp/ldk_node/ | +/// | `log_dir_path` | None | +/// | `network` | Bitcoin | +/// | `listening_address` | None | +/// | `default_cltv_expiry_delta` | 144 | +/// | `onchain_wallet_sync_interval_secs` | 80 | +/// | `wallet_sync_interval_secs` | 30 | +/// | `fee_rate_cache_update_interval_secs` | 600 | +/// | `trusted_peers_0conf` | [] | +/// | `log_level` | Debug | /// pub struct Config { /// The path where the underlying LDK and BDK persist their data. pub storage_dir_path: String, + /// The path where logs are stored. + /// + /// If set to `None`, logs can be found in the `logs` subdirectory in [`Config::storage_dir_path`]. + pub log_dir_path: Option, /// The used Bitcoin network. pub network: Network, /// The IP address and TCP port the node will listen on. @@ -247,6 +252,7 @@ impl Default for Config { fn default() -> Self { Self { storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(), + log_dir_path: None, network: DEFAULT_NETWORK, listening_address: None, default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA, diff --git a/src/logger.rs b/src/logger.rs index 3da9e7e547..b8e0e12de1 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -16,15 +16,19 @@ pub(crate) struct FilesystemLogger { } impl FilesystemLogger { - pub(crate) fn new(file_path: String, level: Level) -> Result { - if let Some(parent_dir) = Path::new(&file_path).parent() { + pub(crate) fn new(log_dir: String, level: Level) -> Result { + let log_file_name = + format!("ldk_node_{}.log", chrono::offset::Local::now().format("%Y_%m_%d")); + let log_file_path = format!("{}/{}", log_dir, log_file_name); + + if let Some(parent_dir) = Path::new(&log_file_path).parent() { fs::create_dir_all(parent_dir).expect("Failed to create log parent directory"); // make sure the file exists, so that the symlink has something to point to. fs::OpenOptions::new() .create(true) .append(true) - .open(file_path.clone()) + .open(log_file_path.clone()) .map_err(|_| ())?; // Create a symlink to the current log file, with prior cleanup @@ -32,10 +36,10 @@ impl FilesystemLogger { if log_file_symlink.as_path().exists() && log_file_symlink.as_path().is_symlink() { fs::remove_file(&log_file_symlink).map_err(|_| ())?; } - symlink(&file_path, &log_file_symlink).map_err(|_| ())?; + symlink(&log_file_name, &log_file_symlink).map_err(|_| ())?; } - Ok(Self { file_path, level }) + Ok(Self { file_path: log_file_path, level }) } } impl Logger for FilesystemLogger {