Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d5d4ce7

Browse files
committedJan 30, 2025
Slightly clean up setup_logger logic
We slightly improve readability of `setup_logger`, and also clean up some of the `DEFAULT` consts while we're at it.
1 parent 83159d0 commit d5d4ce7

File tree

3 files changed

+32
-51
lines changed

3 files changed

+32
-51
lines changed
 

‎src/builder.rs

+28-47
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, Config, EsploraSyncConfig, DEFAULT_LOG_FILE_PATH, DEFAULT_LOG_LEVEL,
11-
DEFAULT_STORAGE_DIR_PATH, WALLET_KEYS_SEED_LEN,
10+
default_user_config, Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
11+
WALLET_KEYS_SEED_LEN,
1212
};
1313

1414
use crate::connection::ConnectionManager;
@@ -111,18 +111,7 @@ impl Default for LiquiditySourceConfig {
111111

112112
#[derive(Clone)]
113113
enum LogWriterConfig {
114-
File {
115-
/// The log file path.
116-
///
117-
/// This specifies the log file path if a destination other than the storage
118-
/// directory, i.e. [`Config::storage_dir_path`], is preferred. If unconfigured,
119-
/// defaults to [`DEFAULT_LOG_FILE_PATH`] in default storage directory.
120-
log_file_path: Option<String>,
121-
/// This specifies the log level.
122-
///
123-
/// If unconfigured, defaults to `Debug`.
124-
log_level: Option<LogLevel>,
125-
},
114+
File { log_file_path: Option<String>, log_level: Option<LogLevel> },
126115
Log(LogLevel),
127116
Custom(Arc<dyn LogWriter>),
128117
}
@@ -143,15 +132,6 @@ impl std::fmt::Debug for LogWriterConfig {
143132
}
144133
}
145134

146-
impl Default for LogWriterConfig {
147-
fn default() -> Self {
148-
Self::File {
149-
log_file_path: Some(DEFAULT_LOG_FILE_PATH.to_string()),
150-
log_level: Some(DEFAULT_LOG_LEVEL),
151-
}
152-
}
153-
}
154-
155135
/// An error encountered during building a [`Node`].
156136
///
157137
/// [`Node`]: crate::Node
@@ -349,9 +329,11 @@ impl NodeBuilder {
349329

350330
/// Configures the [`Node`] instance to write logs to the filesystem.
351331
///
352-
/// The `log_file_path` defaults to the [`DEFAULT_LOG_FILE_PATH`] in the default
353-
/// storage directory if set to None.
354-
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to None.
332+
/// The `log_file_path` defaults to [`DEFAULT_LOG_FILENAME`] in the configured
333+
/// [`Config::storage_dir_path`] if set to `None`.
334+
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to `None`.
335+
///
336+
/// [`DEFAULT_LOG_FILENAME`]: crate::config::DEFAULT_LOG_FILENAME
355337
pub fn set_filesystem_logger(
356338
&mut self, log_file_path: Option<String>, log_level: Option<LogLevel>,
357339
) -> &mut Self {
@@ -674,9 +656,11 @@ impl ArcedNodeBuilder {
674656

675657
/// Configures the [`Node`] instance to write logs to the filesystem.
676658
///
677-
/// The `log_file_path` defaults to the [`DEFAULT_LOG_FILENAME`] in the default
678-
/// storage directory if set to None.
679-
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to None.
659+
/// The `log_file_path` defaults to [`DEFAULT_LOG_FILENAME`] in the configured
660+
/// [`Config::storage_dir_path`] if set to `None`.
661+
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to `None`.
662+
///
663+
/// [`DEFAULT_LOG_FILENAME`]: crate::config::DEFAULT_LOG_FILENAME
680664
pub fn set_filesystem_logger(
681665
&self, log_file_path: Option<String>, log_level: Option<LogLevel>,
682666
) {
@@ -1316,34 +1300,31 @@ fn build_with_store_internal(
13161300
}
13171301

13181302
/// Sets up the node logger.
1319-
///
1320-
/// If `log_writer_conf` is set to None, uses [`LogWriterConfig::default()`].
1321-
/// The `node_conf` is provided to access the configured storage directory.
13221303
fn setup_logger(
1323-
log_writer_conf: &Option<LogWriterConfig>, node_conf: &Config,
1304+
log_writer_config: &Option<LogWriterConfig>, config: &Config,
13241305
) -> Result<Arc<Logger>, BuildError> {
1325-
let is_default = log_writer_conf.is_none();
1326-
let default_lw_config = LogWriterConfig::default();
1327-
let log_writer_config =
1328-
if let Some(conf) = log_writer_conf { conf } else { &default_lw_config };
1329-
13301306
let logger = match log_writer_config {
1331-
LogWriterConfig::File { log_file_path, log_level } => {
1332-
let fp = DEFAULT_LOG_FILE_PATH
1333-
.replace(DEFAULT_STORAGE_DIR_PATH, &node_conf.storage_dir_path);
1334-
let log_file_path =
1335-
if is_default { &fp } else { log_file_path.as_ref().map(|p| p).unwrap_or(&fp) };
1336-
1337-
let log_level = log_level.unwrap_or(DEFAULT_LOG_LEVEL);
1307+
Some(LogWriterConfig::File { log_file_path, log_level }) => {
1308+
let log_file_path = log_file_path
1309+
.clone()
1310+
.unwrap_or_else(|| format!("{}/{}", config.storage_dir_path, DEFAULT_LOG_FILENAME));
1311+
let log_level = log_level.unwrap_or_else(|| DEFAULT_LOG_LEVEL);
13381312

13391313
Logger::new_fs_writer(log_file_path, log_level)
13401314
.map_err(|_| BuildError::LoggerSetupFailed)?
13411315
},
1342-
LogWriterConfig::Log(log_level) => Logger::new_log_facade(*log_level),
1316+
Some(LogWriterConfig::Log(log_level)) => Logger::new_log_facade(*log_level),
13431317

1344-
LogWriterConfig::Custom(custom_log_writer) => {
1318+
Some(LogWriterConfig::Custom(custom_log_writer)) => {
13451319
Logger::new_custom_writer(Arc::clone(&custom_log_writer))
13461320
},
1321+
None => {
1322+
// Default to use `FileWriter`
1323+
let log_file_path = format!("{}/{}", config.storage_dir_path, DEFAULT_LOG_FILENAME);
1324+
let log_level = DEFAULT_LOG_LEVEL;
1325+
Logger::new_fs_writer(log_file_path, log_level)
1326+
.map_err(|_| BuildError::LoggerSetupFailed)?
1327+
},
13471328
};
13481329

13491330
Ok(Arc::new(logger))

‎src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const DEFAULT_ANCHOR_PER_CHANNEL_RESERVE_SATS: u64 = 25_000;
3232
/// The default log level.
3333
pub const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
3434

35-
/// The default log file path.
36-
pub const DEFAULT_LOG_FILE_PATH: &'static str = "/tmp/ldk_node/ldk_node.log";
35+
/// The default log file name.
36+
pub const DEFAULT_LOG_FILENAME: &'static str = "ldk_node.log";
3737

3838
/// The default storage directory.
3939
pub const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node";

‎src/logger.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub(crate) struct Logger {
174174
impl Logger {
175175
/// Creates a new logger with a filesystem writer. The parameters to this function
176176
/// are the path to the log file, and the log level.
177-
pub fn new_fs_writer(file_path: &str, level: LogLevel) -> Result<Self, ()> {
177+
pub fn new_fs_writer(file_path: String, level: LogLevel) -> Result<Self, ()> {
178178
if let Some(parent_dir) = Path::new(&file_path).parent() {
179179
fs::create_dir_all(parent_dir)
180180
.map_err(|e| eprintln!("ERROR: Failed to create log parent directory: {}", e))?;
@@ -187,7 +187,7 @@ impl Logger {
187187
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;
188188
}
189189

190-
Ok(Self { writer: Writer::FileWriter { file_path: file_path.to_string(), level } })
190+
Ok(Self { writer: Writer::FileWriter { file_path, level } })
191191
}
192192

193193
pub fn new_log_facade(level: LogLevel) -> Self {

0 commit comments

Comments
 (0)
Please sign in to comment.