Skip to content

Add the date when the node was started to the name of the logfile. #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
20 changes: 20 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down