From ffe5a0a44242924f3383df3915eeb66352a0abc8 Mon Sep 17 00:00:00 2001 From: Orbital Date: Thu, 22 Jun 2023 19:24:30 -0500 Subject: [PATCH] Change log symlink to be relative --- src/builder.rs | 7 +------ src/logger.rs | 14 +++++++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 382828631..3c02815ba 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -394,13 +394,8 @@ fn build_with_store_internal( }; // Initialize the Logger - let log_file_path = format!( - "{}/ldk_node_{}.log", - log_dir, - 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/logger.rs b/src/logger.rs index 3da9e7e54..b8e0e12de 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 {