Skip to content

Commit 5a59240

Browse files
authored
Merge pull request #129 from orbitalturtle/set-log-dir
Add a builder method to customize where logs are stored
2 parents 3c7dac9 + 3a31209 commit 5a59240

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ldk_node {
44

55
dictionary Config {
66
string storage_dir_path = "/tmp/ldk_node/";
7+
string? log_dir_path = null;
78
Network network = "Bitcoin";
89
NetAddress? listening_address = null;
910
u32 default_cltv_expiry_delta = 144;

src/builder.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ impl NodeBuilder {
202202
self
203203
}
204204

205+
/// Sets the log dir path if logs need to live separate from the storage directory path.
206+
pub fn set_log_dir_path(&mut self, log_dir_path: String) -> &mut Self {
207+
self.config.log_dir_path = Some(log_dir_path);
208+
self
209+
}
210+
205211
/// Sets the Bitcoin network used.
206212
pub fn set_network(&mut self, network: Network) -> &mut Self {
207213
self.config.network = network;
@@ -329,6 +335,11 @@ impl ArcedNodeBuilder {
329335
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path);
330336
}
331337

338+
/// Sets the log dir path if logs need to live separate from the storage directory path.
339+
pub fn set_log_dir_path(&self, log_dir_path: String) {
340+
self.inner.write().unwrap().set_log_dir_path(log_dir_path);
341+
}
342+
332343
/// Sets the Bitcoin network used.
333344
pub fn set_network(&self, network: Network) {
334345
self.inner.write().unwrap().set_network(network);
@@ -377,14 +388,14 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
377388
let bdk_data_dir = format!("{}/bdk", config.storage_dir_path);
378389
fs::create_dir_all(bdk_data_dir.clone()).map_err(|_| BuildError::StoragePathAccessFailed)?;
379390

391+
let log_dir = match &config.log_dir_path {
392+
Some(log_dir) => String::from(log_dir),
393+
None => config.storage_dir_path.clone() + "/logs",
394+
};
395+
380396
// Initialize the Logger
381-
let log_file_path = format!(
382-
"{}/logs/ldk_node_{}.log",
383-
config.storage_dir_path,
384-
chrono::offset::Local::now().format("%Y_%m_%d")
385-
);
386397
let logger = Arc::new(
387-
FilesystemLogger::new(log_file_path.clone(), config.log_level)
398+
FilesystemLogger::new(log_dir, config.log_level)
388399
.map_err(|_| BuildError::LoggerSetupFailed)?,
389400
);
390401

src/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,26 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
198198
///
199199
/// ### Defaults
200200
///
201-
/// | Parameter | Value |
202-
/// |----------------------------------------|------------------|
203-
/// | `storage_dir_path` | /tmp/ldk_node/ |
204-
/// | `network` | Bitcoin |
205-
/// | `listening_address` | None |
206-
/// | `default_cltv_expiry_delta` | 144 |
207-
/// | `onchain_wallet_sync_interval_secs` | 80 |
208-
/// | `wallet_sync_interval_secs` | 30 |
209-
/// | `fee_rate_cache_update_interval_secs` | 600 |
210-
/// | `trusted_peers_0conf` | [] |
211-
/// | `log_level` | Debug |
201+
/// | Parameter | Value |
202+
/// |----------------------------------------|--------------------|
203+
/// | `storage_dir_path` | /tmp/ldk_node/ |
204+
/// | `log_dir_path` | None |
205+
/// | `network` | Bitcoin |
206+
/// | `listening_address` | None |
207+
/// | `default_cltv_expiry_delta` | 144 |
208+
/// | `onchain_wallet_sync_interval_secs` | 80 |
209+
/// | `wallet_sync_interval_secs` | 30 |
210+
/// | `fee_rate_cache_update_interval_secs` | 600 |
211+
/// | `trusted_peers_0conf` | [] |
212+
/// | `log_level` | Debug |
212213
///
213214
pub struct Config {
214215
/// The path where the underlying LDK and BDK persist their data.
215216
pub storage_dir_path: String,
217+
/// The path where logs are stored.
218+
///
219+
/// If set to `None`, logs can be found in the `logs` subdirectory in [`Config::storage_dir_path`].
220+
pub log_dir_path: Option<String>,
216221
/// The used Bitcoin network.
217222
pub network: Network,
218223
/// The IP address and TCP port the node will listen on.
@@ -247,6 +252,7 @@ impl Default for Config {
247252
fn default() -> Self {
248253
Self {
249254
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
255+
log_dir_path: None,
250256
network: DEFAULT_NETWORK,
251257
listening_address: None,
252258
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,

src/logger.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ pub(crate) struct FilesystemLogger {
1616
}
1717

1818
impl FilesystemLogger {
19-
pub(crate) fn new(file_path: String, level: Level) -> Result<Self, ()> {
20-
if let Some(parent_dir) = Path::new(&file_path).parent() {
19+
pub(crate) fn new(log_dir: String, level: Level) -> Result<Self, ()> {
20+
let log_file_name =
21+
format!("ldk_node_{}.log", chrono::offset::Local::now().format("%Y_%m_%d"));
22+
let log_file_path = format!("{}/{}", log_dir, log_file_name);
23+
24+
if let Some(parent_dir) = Path::new(&log_file_path).parent() {
2125
fs::create_dir_all(parent_dir).expect("Failed to create log parent directory");
2226

2327
// make sure the file exists, so that the symlink has something to point to.
2428
fs::OpenOptions::new()
2529
.create(true)
2630
.append(true)
27-
.open(file_path.clone())
31+
.open(log_file_path.clone())
2832
.map_err(|_| ())?;
2933

3034
// Create a symlink to the current log file, with prior cleanup
3135
let log_file_symlink = parent_dir.join("ldk_node_latest.log");
3236
if log_file_symlink.as_path().exists() && log_file_symlink.as_path().is_symlink() {
3337
fs::remove_file(&log_file_symlink).map_err(|_| ())?;
3438
}
35-
symlink(&file_path, &log_file_symlink).map_err(|_| ())?;
39+
symlink(&log_file_name, &log_file_symlink).map_err(|_| ())?;
3640
}
3741

38-
Ok(Self { file_path, level })
42+
Ok(Self { file_path: log_file_path, level })
3943
}
4044
}
4145
impl Logger for FilesystemLogger {

0 commit comments

Comments
 (0)