Skip to content

Commit 60dffa8

Browse files
authored
Merge pull request #394 from enigbe/revert-filesystem-logging-to-simple-file
revert: removes date from & symlink to log files
2 parents 69d03c0 + f033ba2 commit 60dffa8

File tree

5 files changed

+24
-38
lines changed

5 files changed

+24
-38
lines changed

bindings/ldk_node.udl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ldk_node {
55

66
dictionary Config {
77
string storage_dir_path;
8-
string? log_dir_path;
8+
string? log_file_path;
99
Network network;
1010
sequence<SocketAddress>? listening_addresses;
1111
NodeAlias? node_alias;

src/builder.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ impl NodeBuilder {
298298
self
299299
}
300300

301-
/// Sets the log dir path if logs need to live separate from the storage directory path.
302-
pub fn set_log_dir_path(&mut self, log_dir_path: String) -> &mut Self {
303-
self.config.log_dir_path = Some(log_dir_path);
301+
/// Sets the log file path if the log file needs to live separate from the storage directory path.
302+
pub fn set_log_file_path(&mut self, log_dir_path: String) -> &mut Self {
303+
self.config.log_file_path = Some(log_dir_path);
304304
self
305305
}
306306

@@ -610,9 +610,9 @@ impl ArcedNodeBuilder {
610610
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path);
611611
}
612612

613-
/// Sets the log dir path if logs need to live separate from the storage directory path.
614-
pub fn set_log_dir_path(&self, log_dir_path: String) {
615-
self.inner.write().unwrap().set_log_dir_path(log_dir_path);
613+
/// Sets the log file path if logs need to live separate from the storage directory path.
614+
pub fn set_log_file_path(&self, log_file_path: String) {
615+
self.inner.write().unwrap().set_log_file_path(log_file_path);
616616
}
617617

618618
/// Sets the Bitcoin network used.
@@ -1231,14 +1231,16 @@ fn build_with_store_internal(
12311231
})
12321232
}
12331233

1234+
/// Sets up the node logger, creating a new log file if it does not exist, or utilizing
1235+
/// the existing log file.
12341236
fn setup_logger(config: &Config) -> Result<Arc<FilesystemLogger>, BuildError> {
1235-
let log_dir = match &config.log_dir_path {
1237+
let log_file_path = match &config.log_file_path {
12361238
Some(log_dir) => String::from(log_dir),
1237-
None => config.storage_dir_path.clone() + "/logs",
1239+
None => format!("{}/{}", config.storage_dir_path.clone(), "ldk_node.log"),
12381240
};
12391241

12401242
Ok(Arc::new(
1241-
FilesystemLogger::new(log_dir, config.log_level)
1243+
FilesystemLogger::new(log_file_path, config.log_level)
12421244
.map_err(|_| BuildError::LoggerSetupFailed)?,
12431245
))
12441246
}

src/config.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ pub struct Config {
105105
pub storage_dir_path: String,
106106
/// The path where logs are stored.
107107
///
108-
/// If set to `None`, logs can be found in the `logs` subdirectory in [`Config::storage_dir_path`].
109-
pub log_dir_path: Option<String>,
108+
/// If set to `None`, logs can be found in `ldk_node.log` in the [`Config::storage_dir_path`]
109+
/// directory.
110+
pub log_file_path: Option<String>,
110111
/// The used Bitcoin network.
111112
pub network: Network,
112113
/// The addresses on which the node will listen for incoming connections.
@@ -167,7 +168,7 @@ impl Default for Config {
167168
fn default() -> Self {
168169
Self {
169170
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
170-
log_dir_path: None,
171+
log_file_path: None,
171172
network: DEFAULT_NETWORK,
172173
listening_addresses: None,
173174
trusted_peers_0conf: Vec::new(),

src/logger.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use chrono::Utc;
1414

1515
use std::fs;
1616
use std::io::Write;
17-
#[cfg(not(target_os = "windows"))]
18-
use std::os::unix::fs::symlink;
1917
use std::path::Path;
2018

2119
pub(crate) struct FilesystemLogger {
@@ -24,33 +22,18 @@ pub(crate) struct FilesystemLogger {
2422
}
2523

2624
impl FilesystemLogger {
27-
pub(crate) fn new(log_dir: String, level: Level) -> Result<Self, ()> {
28-
let log_file_name =
29-
format!("ldk_node_{}.log", chrono::offset::Local::now().format("%Y_%m_%d"));
30-
let log_file_path = format!("{}/{}", log_dir, log_file_name);
31-
25+
/// Creates a new filesystem logger given the path to the log file and the log level.
26+
pub(crate) fn new(log_file_path: String, level: Level) -> Result<Self, ()> {
3227
if let Some(parent_dir) = Path::new(&log_file_path).parent() {
33-
fs::create_dir_all(parent_dir).expect("Failed to create log parent directory");
28+
fs::create_dir_all(parent_dir)
29+
.map_err(|e| eprintln!("ERROR: Failed to create log parent directory: {}", e))?;
3430

35-
// make sure the file exists, so that the symlink has something to point to.
31+
// make sure the file exists.
3632
fs::OpenOptions::new()
3733
.create(true)
3834
.append(true)
39-
.open(log_file_path.clone())
35+
.open(&log_file_path)
4036
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;
41-
42-
#[cfg(not(target_os = "windows"))]
43-
{
44-
// Create a symlink to the current log file, with prior cleanup
45-
let log_file_symlink = parent_dir.join("ldk_node_latest.log");
46-
if log_file_symlink.as_path().is_symlink() {
47-
fs::remove_file(&log_file_symlink).map_err(|e| {
48-
eprintln!("ERROR: Failed to remove log file symlink: {}", e)
49-
})?;
50-
}
51-
symlink(&log_file_name, &log_file_symlink)
52-
.map_err(|e| eprintln!("ERROR: Failed to create log file symlink: {}", e))?;
53-
}
5437
}
5538

5639
Ok(Self { file_path: log_file_path, level })

tests/integration_tests_rust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ fn start_stop_reinit() {
234234
node.sync_wallets().unwrap();
235235
assert_eq!(node.list_balances().spendable_onchain_balance_sats, expected_amount.to_sat());
236236

237-
let log_file_symlink = format!("{}/logs/ldk_node_latest.log", config.clone().storage_dir_path);
238-
assert!(std::path::Path::new(&log_file_symlink).is_symlink());
237+
let log_file = format!("{}/ldk_node.log", config.clone().storage_dir_path);
238+
assert!(std::path::Path::new(&log_file).exists());
239239

240240
node.stop().unwrap();
241241
assert_eq!(node.stop(), Err(NodeError::NotRunning));

0 commit comments

Comments
 (0)