Skip to content

Commit b3761eb

Browse files
committed
Add customizable logging to configuration.
1 parent 64346b8 commit b3761eb

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

src/builder.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
// accordance with one or both of these licenses.
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
9-
use crate::config::{default_user_config, Config, EsploraSyncConfig, WALLET_KEYS_SEED_LEN};
9+
use crate::config::{
10+
default_user_config, Config, EsploraSyncConfig, LoggingConfig, WALLET_KEYS_SEED_LEN,
11+
};
1012

1113
use crate::connection::ConnectionManager;
1214
use crate::event::EventQueue;
@@ -29,8 +31,8 @@ use crate::types::{
2931
};
3032
use crate::wallet::persist::KVStoreWalletPersister;
3133
use crate::wallet::Wallet;
34+
use crate::Node;
3235
use crate::{io, NodeMetrics};
33-
use crate::{LogLevel, Node};
3436

3537
use lightning::chain::{chainmonitor, BestBlock, Watch};
3638
use lightning::io::Cursor;
@@ -300,12 +302,6 @@ impl NodeBuilder {
300302
self
301303
}
302304

303-
/// Sets the log dir path if logs need to live separate from the storage directory path.
304-
pub fn set_log_dir_path(&mut self, log_dir_path: String) -> &mut Self {
305-
self.config.log_dir_path = Some(log_dir_path);
306-
self
307-
}
308-
309305
/// Sets the Bitcoin network used.
310306
pub fn set_network(&mut self, network: Network) -> &mut Self {
311307
self.config.network = network;
@@ -335,12 +331,6 @@ impl NodeBuilder {
335331
Ok(self)
336332
}
337333

338-
/// Sets the level at which [`Node`] will log messages.
339-
pub fn set_log_level(&mut self, level: LogLevel) -> &mut Self {
340-
self.config.log_level = level;
341-
self
342-
}
343-
344334
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
345335
/// previously configured.
346336
pub fn build(&self) -> Result<Node, BuildError> {
@@ -1234,20 +1224,21 @@ fn build_with_store_internal(
12341224
}
12351225

12361226
fn setup_logger(config: &Config) -> Result<Arc<LdkNodeLogger>, BuildError> {
1237-
let log_dir = match &config.log_dir_path {
1238-
Some(log_dir) => String::from(log_dir),
1239-
None => config.storage_dir_path.clone() + "/logs",
1240-
};
1241-
let filesystem_log_writer =
1242-
FilesystemLogWriter::new(log_dir.clone()).map_err(|_| BuildError::LoggerSetupFailed)?;
1243-
Ok(Arc::new(
1244-
LdkNodeLogger::new(
1245-
config.log_level,
1246-
Box::new(default_format),
1247-
Box::new(move |s| filesystem_log_writer.write(s)),
1248-
)
1249-
.map_err(|_| BuildError::LoggerSetupFailed)?,
1250-
))
1227+
match config.logging_config {
1228+
LoggingConfig::Custom(ref logger) => Ok(logger.clone()),
1229+
LoggingConfig::Filesystem { ref log_dir, log_level } => {
1230+
let filesystem_log_writer = FilesystemLogWriter::new(log_dir.clone())
1231+
.map_err(|_| BuildError::LoggerSetupFailed)?;
1232+
Ok(Arc::new(
1233+
LdkNodeLogger::new(
1234+
log_level,
1235+
Box::new(default_format),
1236+
Box::new(move |s| filesystem_log_writer.write(s)),
1237+
)
1238+
.map_err(|_| BuildError::LoggerSetupFailed)?,
1239+
))
1240+
},
1241+
}
12511242
}
12521243

12531244
fn seed_bytes_from_config(

src/config.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
//! Objects for configuring the node.
99
10+
use crate::logger::LdkNodeLogger;
1011
use crate::payment::SendingParameters;
1112

1213
use lightning::ln::msgs::SocketAddress;
@@ -105,8 +106,9 @@ pub struct Config {
105106
pub storage_dir_path: String,
106107
/// The path where logs are stored.
107108
///
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>,
109+
/// In the default configuration logs can be found in the `logs` subdirectory in
110+
/// [`Config::storage_dir_path`], and the log level is set to [`DEFAULT_LOG_LEVEL`].
111+
pub logging_config: LoggingConfig,
110112
/// The used Bitcoin network.
111113
pub network: Network,
112114
/// The addresses on which the node will listen for incoming connections.
@@ -132,10 +134,6 @@ pub struct Config {
132134
/// Channels with available liquidity less than the required amount times this value won't be
133135
/// used to send pre-flight probes.
134136
pub probing_liquidity_limit_multiplier: u64,
135-
/// The level at which we log messages.
136-
///
137-
/// Any messages below this level will be excluded from the logs.
138-
pub log_level: LogLevel,
139137
/// Configuration options pertaining to Anchor channels, i.e., channels for which the
140138
/// `option_anchors_zero_fee_htlc_tx` channel type is negotiated.
141139
///
@@ -167,19 +165,46 @@ impl Default for Config {
167165
fn default() -> Self {
168166
Self {
169167
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
170-
log_dir_path: None,
168+
logging_config: LoggingConfig::default(),
171169
network: DEFAULT_NETWORK,
172170
listening_addresses: None,
173171
trusted_peers_0conf: Vec::new(),
174172
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
175-
log_level: DEFAULT_LOG_LEVEL,
176173
anchor_channels_config: Some(AnchorChannelsConfig::default()),
177174
sending_parameters: None,
178175
node_alias: None,
179176
}
180177
}
181178
}
182179

180+
/// Configuration options for logging.
181+
#[derive(Debug, Clone)]
182+
pub enum LoggingConfig {
183+
/// An opinionated filesystem logger.
184+
///
185+
/// This logger will always write at `{log_dir}/ldk_node_latest.log`, which is a symlink to the
186+
/// most recent log file, which is created and timestamped at initialization.
187+
Filesystem {
188+
/// The absolute path where logs are stored.
189+
log_dir: String,
190+
/// The level at which we log messages.
191+
///
192+
/// Any messages below this level will be excluded from the logs.
193+
log_level: LogLevel,
194+
},
195+
/// A custom logger.
196+
Custom(std::sync::Arc<LdkNodeLogger>),
197+
}
198+
199+
impl Default for LoggingConfig {
200+
fn default() -> Self {
201+
Self::Filesystem {
202+
log_dir: format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, "logs"),
203+
log_level: DEFAULT_LOG_LEVEL,
204+
}
205+
}
206+
}
207+
183208
/// Configuration options pertaining to 'Anchor' channels, i.e., channels for which the
184209
/// `option_anchors_zero_fee_htlc_tx` channel type is negotiated.
185210
///

0 commit comments

Comments
 (0)