Skip to content

Commit 97d238c

Browse files
authored
Merge pull request #116 from ulrichard/feature/log_per_date
Add the date when the node was started to the name of the logfile.
2 parents ffa2387 + 2f087c8 commit 97d238c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,12 @@ impl Builder {
417417
let config = Arc::new(self.config.read().unwrap().clone());
418418

419419
// Initialize the Logger
420-
let log_file_path = format!("{}/ldk_node.log", config.storage_dir_path);
421-
let logger = Arc::new(FilesystemLogger::new(log_file_path, config.log_level));
420+
let log_file_path = format!(
421+
"{}/logs/ldk_node_{}.log",
422+
config.storage_dir_path,
423+
chrono::offset::Local::now().format("%Y_%m_%d")
424+
);
425+
let logger = Arc::new(FilesystemLogger::new(log_file_path.clone(), config.log_level));
422426

423427
// Initialize the on-chain wallet and chain access
424428
let seed_bytes = match &*self.entropy_source_config.read().unwrap() {

src/logger.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use lightning::util::ser::Writer;
77
use chrono::Utc;
88

99
use std::fs;
10+
use std::os::unix::fs::symlink;
1011
use std::path::Path;
1112

1213
pub(crate) struct FilesystemLogger {
@@ -18,7 +19,26 @@ impl FilesystemLogger {
1819
pub(crate) fn new(file_path: String, level: Level) -> Self {
1920
if let Some(parent_dir) = Path::new(&file_path).parent() {
2021
fs::create_dir_all(parent_dir).expect("Failed to create log parent directory");
22+
23+
// make sure the file exists, so that the symlink has something to point to.
24+
fs::OpenOptions::new()
25+
.create(true)
26+
.append(true)
27+
.open(file_path.clone())
28+
.expect("Failed to open log file");
29+
30+
// Create a symlink to the current log file, with prior cleanup
31+
let log_file_symlink = parent_dir.join("ldk_node_latest.log");
32+
if log_file_symlink.as_path().exists() && log_file_symlink.as_path().is_symlink() {
33+
fs::remove_file(&log_file_symlink)
34+
.expect("Failed to remove an old symlink for the log file");
35+
}
36+
symlink(&file_path, &log_file_symlink).expect(&format!(
37+
"Failed to create symlink for the log file: {:?}",
38+
log_file_symlink
39+
));
2140
}
41+
2242
Self { file_path, level }
2343
}
2444
}

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!("{}/logs/ldk_node_latest.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)