diff --git a/src/lib.rs b/src/lib.rs index 85b085028..040c57c28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -417,8 +417,12 @@ impl Builder { let config = Arc::new(self.config.read().unwrap().clone()); // Initialize the Logger - let log_file_path = format!("{}/ldk_node.log", config.storage_dir_path); - let logger = Arc::new(FilesystemLogger::new(log_file_path, config.log_level)); + 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)); // Initialize the on-chain wallet and chain access let seed_bytes = match &*self.entropy_source_config.read().unwrap() { diff --git a/src/logger.rs b/src/logger.rs index ae4e7a17f..0c4df2198 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -7,6 +7,7 @@ use lightning::util::ser::Writer; use chrono::Utc; use std::fs; +use std::os::unix::fs::symlink; use std::path::Path; pub(crate) struct FilesystemLogger { @@ -18,7 +19,26 @@ impl FilesystemLogger { pub(crate) fn new(file_path: String, level: Level) -> Self { if let Some(parent_dir) = Path::new(&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()) + .expect("Failed to open log file"); + + // Create a symlink to the current log file, with prior cleanup + let log_file_symlink = parent_dir.join("ldk_node_latest.log"); + if log_file_symlink.as_path().exists() && log_file_symlink.as_path().is_symlink() { + fs::remove_file(&log_file_symlink) + .expect("Failed to remove an old symlink for the log file"); + } + symlink(&file_path, &log_file_symlink).expect(&format!( + "Failed to create symlink for the log file: {:?}", + log_file_symlink + )); } + Self { file_path, level } } } diff --git a/src/test/functional_tests.rs b/src/test/functional_tests.rs index dcacd2631..04fb2c4fe 100644 --- a/src/test/functional_tests.rs +++ b/src/test/functional_tests.rs @@ -310,6 +310,9 @@ fn start_stop_reinit() { node.sync_wallets().unwrap(); assert_eq!(node.onchain_balance().unwrap().get_spendable(), expected_amount.to_sat()); + let log_file_symlink = format!("{}/logs/ldk_node_latest.log", config.storage_dir_path); + assert!(std::path::Path::new(&log_file_symlink).is_symlink()); + node.stop().unwrap(); assert_eq!(node.stop(), Err(Error::NotRunning));