Skip to content

Commit

Permalink
Change log symlink to be relative
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Jul 5, 2023
1 parent bf0c851 commit 3a31209
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
7 changes: 1 addition & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,8 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
};

// 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)?,
);

Expand Down
14 changes: 9 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@ pub(crate) struct FilesystemLogger {
}

impl FilesystemLogger {
pub(crate) fn new(file_path: String, level: Level) -> Result<Self, ()> {
if let Some(parent_dir) = Path::new(&file_path).parent() {
pub(crate) fn new(log_dir: String, level: Level) -> Result<Self, ()> {
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
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).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 {
Expand Down

0 comments on commit 3a31209

Please sign in to comment.