Skip to content

Commit ba3fc5c

Browse files
committed
Add the date when the node was started to the name of the logfile.
Create a symlink with the old log name to the current log file. This makes it easier to delete old logs
1 parent ffa2387 commit ba3fc5c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-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: 29 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,35 @@ 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+
if let Some(parent_dir) = Path::new(&parent_dir).parent() {
24+
// make sure the file exists, so that the symlink has something to point to.
25+
fs::OpenOptions::new()
26+
.create(true)
27+
.append(true)
28+
.open(file_path.clone())
29+
.expect("Failed to open log file");
30+
31+
// Create a symlink to the current log file, with prior cleanup
32+
let log_file_symlink = parent_dir.join("ldk_node.log");
33+
if log_file_symlink.as_path().exists() {
34+
if log_file_symlink.as_path().is_symlink() {
35+
fs::remove_file(&log_file_symlink)
36+
.expect("Failed to remove an old symlink for the log file");
37+
} else {
38+
let log_file_legacy =
39+
format!("{}logs/ldk_node.log", parent_dir.to_string_lossy());
40+
fs::rename(&file_path, log_file_legacy)
41+
.expect("Failed to rename the old log file");
42+
}
43+
}
44+
symlink(&file_path, &log_file_symlink).expect(&format!(
45+
"Failed to create symlink for the log file: {:?}",
46+
log_file_symlink
47+
));
48+
}
2149
}
50+
2251
Self { file_path, level }
2352
}
2453
}

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