Skip to content

Commit cef2e4b

Browse files
committed
Add the date when the node was started to the name of the logfile.
Create a symlink with the old log name to the current log file. This makes it easier to delete old logs
1 parent 427e74c commit cef2e4b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ use std::convert::TryInto;
161161
use std::default::Default;
162162
use std::fs;
163163
use std::net::ToSocketAddrs;
164+
use std::os::unix::fs::symlink;
165+
use std::path::Path;
164166
use std::sync::atomic::{AtomicBool, Ordering};
165167
use std::sync::{Arc, Mutex, RwLock};
166168
use std::time::{Duration, Instant, SystemTime};
@@ -384,8 +386,19 @@ impl Builder {
384386
fs::create_dir_all(bdk_data_dir.clone()).expect("Failed to create BDK data directory");
385387

386388
// Initialize the Logger
387-
let log_file_path = format!("{}/ldk_node.log", config.storage_dir_path);
388-
let logger = Arc::new(FilesystemLogger::new(log_file_path, config.log_level));
389+
let log_file_path = format!(
390+
"{}/logs/ldk_node_{}.log",
391+
config.storage_dir_path,
392+
chrono::offset::Local::now().format("%Y_%m_%d")
393+
);
394+
let logger = Arc::new(FilesystemLogger::new(log_file_path.clone(), config.log_level));
395+
let log_file_symlink = format!("{}/ldk_node.log", config.storage_dir_path);
396+
if Path::new(&log_file_symlink).exists() {
397+
fs::remove_file(&log_file_symlink)
398+
.expect("Failed to remove an old symlink for the log file");
399+
}
400+
symlink(&log_file_path, &log_file_symlink)
401+
.expect(&format!("Failed to create symlink for the log file: {}", log_file_symlink));
389402

390403
// Initialize the on-chain wallet and chain access
391404
let seed_bytes = match &*self.entropy_source_config.read().unwrap() {
@@ -1743,7 +1756,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
17431756
/// # use ldk_node::bitcoin::Network;
17441757
/// # let mut config = Config::default();
17451758
/// # config.network = Network::Regtest;
1746-
/// # config.storage_dir_path = "/tmp/ldk_node_test/".to_string();
1759+
/// # config.storage_dir_path = "/tmp/ldk_node_test_list_payments".to_string();
17471760
/// # let builder = Builder::from_config(config);
17481761
/// # let node = builder.build();
17491762
/// node.list_payments_with_filter(|p| p.direction == PaymentDirection::Outbound);

src/test/functional_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ fn start_stop_reinit() {
310310
node.sync_wallets().unwrap();
311311
assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat());
312312

313+
let log_file_symlink = format!("{}/ldk_node.log", config.storage_dir_path);
314+
assert!(std::path::Path::new(&log_file_symlink).is_symlink());
315+
313316
node.stop().unwrap();
314317
assert_eq!(node.stop(), Err(Error::NotRunning));
315318

0 commit comments

Comments
 (0)