-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add customizable logging system #401
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
// accordance with one or both of these licenses. | ||
|
||
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL}; | ||
use crate::config::{default_user_config, Config, EsploraSyncConfig, WALLET_KEYS_SEED_LEN}; | ||
use crate::config::{ | ||
default_user_config, Config, EsploraSyncConfig, FormatterConfig, LoggerConfig, WriterConfig, | ||
WriterType, WALLET_KEYS_SEED_LEN, | ||
}; | ||
|
||
use crate::connection::ConnectionManager; | ||
use crate::event::EventQueue; | ||
|
@@ -16,7 +19,7 @@ use crate::io::sqlite_store::SqliteStore; | |
use crate::io::utils::{read_node_metrics, write_node_metrics}; | ||
use crate::io::vss_store::VssStore; | ||
use crate::liquidity::LiquiditySource; | ||
use crate::logger::{log_error, log_info, FilesystemLogger, Logger}; | ||
use crate::logger::{build_formatter, log_error, log_info, LdkNodeLogger, Logger, Writer}; | ||
use crate::message_handler::NodeCustomMessageHandler; | ||
use crate::payment::store::PaymentStore; | ||
use crate::peer_store::PeerStore; | ||
|
@@ -27,8 +30,8 @@ use crate::types::{ | |
}; | ||
use crate::wallet::persist::KVStoreWalletPersister; | ||
use crate::wallet::Wallet; | ||
use crate::Node; | ||
use crate::{io, NodeMetrics}; | ||
use crate::{LogLevel, Node}; | ||
|
||
use lightning::chain::{chainmonitor, BestBlock, Watch}; | ||
use lightning::io::Cursor; | ||
|
@@ -298,9 +301,15 @@ impl NodeBuilder { | |
self | ||
} | ||
|
||
/// Sets the log file path if the log file needs to live separate from the storage directory path. | ||
pub fn set_log_file_path(&mut self, log_dir_path: String) -> &mut Self { | ||
self.config.log_file_path = Some(log_dir_path); | ||
/// Sets the logger's writer config. | ||
pub fn set_log_writer_config(&mut self, writer_config: WriterConfig) -> &mut Self { | ||
self.config.logger_config.writer = writer_config; | ||
self | ||
} | ||
|
||
/// Sets the logger's formatter config. | ||
pub fn set_log_formatter_config(&mut self, formatter_config: FormatterConfig) -> &mut Self { | ||
self.config.logger_config.formatter = formatter_config; | ||
self | ||
} | ||
|
||
|
@@ -333,12 +342,6 @@ impl NodeBuilder { | |
Ok(self) | ||
} | ||
|
||
/// Sets the level at which [`Node`] will log messages. | ||
pub fn set_log_level(&mut self, level: LogLevel) -> &mut Self { | ||
self.config.log_level = level; | ||
self | ||
} | ||
|
||
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options | ||
/// previously configured. | ||
pub fn build(&self) -> Result<Node, BuildError> { | ||
|
@@ -391,7 +394,7 @@ impl NodeBuilder { | |
) -> Result<Node, BuildError> { | ||
use bitcoin::key::Secp256k1; | ||
|
||
let logger = setup_logger(&self.config)?; | ||
let logger = setup_logger(&self.config.logger_config)?; | ||
|
||
let seed_bytes = seed_bytes_from_config( | ||
&self.config, | ||
|
@@ -456,7 +459,7 @@ impl NodeBuilder { | |
pub fn build_with_vss_store_and_header_provider( | ||
&self, vss_url: String, store_id: String, header_provider: Arc<dyn VssHeaderProvider>, | ||
) -> Result<Node, BuildError> { | ||
let logger = setup_logger(&self.config)?; | ||
let logger = setup_logger(&self.config.logger_config)?; | ||
|
||
let seed_bytes = seed_bytes_from_config( | ||
&self.config, | ||
|
@@ -488,7 +491,7 @@ impl NodeBuilder { | |
|
||
/// Builds a [`Node`] instance according to the options previously configured. | ||
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Node, BuildError> { | ||
let logger = setup_logger(&self.config)?; | ||
let logger = setup_logger(&self.config.logger_config)?; | ||
let seed_bytes = seed_bytes_from_config( | ||
&self.config, | ||
self.entropy_source_config.as_ref(), | ||
|
@@ -610,9 +613,14 @@ impl ArcedNodeBuilder { | |
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path); | ||
} | ||
|
||
/// Sets the log file path if logs need to live separate from the storage directory path. | ||
pub fn set_log_file_path(&self, log_file_path: String) { | ||
self.inner.write().unwrap().set_log_file_path(log_file_path); | ||
/// Sets the logger's writer config. | ||
pub fn set_log_writer_config(&mut self, writer_config: WriterConfig) -> &mut Self { | ||
self.inner.write().unwrap().set_log_writer_config(writer_config); | ||
} | ||
|
||
/// Sets the logger's formatter config. | ||
pub fn set_log_formatter_config(&mut self, formatter_config: FormatterConfig) -> &mut Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite sure why we'd need separate configurations for the formatter? This is still related to a filesystem writer only, no? |
||
self.inner.write().unwrap().set_log_formatter_config(formatter_config); | ||
} | ||
|
||
/// Sets the Bitcoin network used. | ||
|
@@ -635,11 +643,6 @@ impl ArcedNodeBuilder { | |
self.inner.write().unwrap().set_node_alias(node_alias).map(|_| ()) | ||
} | ||
|
||
/// Sets the level at which [`Node`] will log messages. | ||
pub fn set_log_level(&self, level: LogLevel) { | ||
self.inner.write().unwrap().set_log_level(level); | ||
} | ||
|
||
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options | ||
/// previously configured. | ||
pub fn build(&self) -> Result<Arc<Node>, BuildError> { | ||
|
@@ -734,7 +737,7 @@ fn build_with_store_internal( | |
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>, | ||
gossip_source_config: Option<&GossipSourceConfig>, | ||
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64], | ||
logger: Arc<FilesystemLogger>, kv_store: Arc<DynStore>, | ||
logger: Arc<LdkNodeLogger>, kv_store: Arc<DynStore>, | ||
) -> Result<Node, BuildError> { | ||
// Initialize the status fields. | ||
let is_listening = Arc::new(AtomicBool::new(false)); | ||
|
@@ -1231,23 +1234,28 @@ fn build_with_store_internal( | |
}) | ||
} | ||
|
||
/// Sets up the node logger, creating a new log file if it does not exist, or utilizing | ||
/// the existing log file. | ||
fn setup_logger(config: &Config) -> Result<Arc<FilesystemLogger>, BuildError> { | ||
let log_file_path = match &config.log_file_path { | ||
Some(log_dir) => String::from(log_dir), | ||
None => format!("{}/{}", config.storage_dir_path.clone(), "ldk_node.log"), | ||
/// Sets up the node logger. | ||
fn setup_logger(config: &LoggerConfig) -> Result<Arc<LdkNodeLogger>, BuildError> { | ||
let level = match &config.writer.writer_type { | ||
WriterType::File(file_writer_config) => file_writer_config.level, | ||
WriterType::LogRelay(log_relay_writer_config) => log_relay_writer_config.level, | ||
WriterType::Custom(custom_writer_config) => custom_writer_config.level, | ||
}; | ||
|
||
Ok(Arc::new( | ||
FilesystemLogger::new(log_file_path, config.log_level) | ||
.map_err(|_| BuildError::LoggerSetupFailed)?, | ||
)) | ||
let writer = | ||
Writer::new(&config.writer.writer_type).map_err(|_e| BuildError::LoggerSetupFailed)?; | ||
|
||
let formatter = build_formatter(config.formatter.clone()); | ||
|
||
let ldk_node_logger = | ||
LdkNodeLogger::new(level, formatter, writer).map_err(|_e| BuildError::LoggerSetupFailed)?; | ||
|
||
Ok(Arc::new(ldk_node_logger)) | ||
} | ||
|
||
fn seed_bytes_from_config( | ||
config: &Config, entropy_source_config: Option<&EntropySourceConfig>, | ||
logger: Arc<FilesystemLogger>, | ||
logger: Arc<LdkNodeLogger>, | ||
) -> Result<[u8; 64], BuildError> { | ||
match entropy_source_config { | ||
Some(EntropySourceConfig::SeedBytes(bytes)) => Ok(bytes.clone()), | ||
|
@@ -1269,7 +1277,7 @@ fn seed_bytes_from_config( | |
} | ||
|
||
fn derive_vss_xprv( | ||
config: Arc<Config>, seed_bytes: &[u8; 64], logger: Arc<FilesystemLogger>, | ||
config: Arc<Config>, seed_bytes: &[u8; 64], logger: Arc<LdkNodeLogger>, | ||
) -> Result<Xpriv, BuildError> { | ||
use bitcoin::key::Secp256k1; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use any features in LDK Node so far (mostly to keep ~feature-parity of the Rust vs bindings API, for which everything needs to be configurable via the
Builder
), so please drop this and just take a non-optional dependency onlog
for now.