Skip to content

Commit

Permalink
feat: forward logs to log facade
Browse files Browse the repository at this point in the history
  • Loading branch information
enigbe committed Nov 19, 2024
1 parent 05ffc55 commit 82ffcbf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ libc = "0.2"
uniffi = { version = "0.27.3", features = ["build"], optional = true }
serde = { version = "1.0.210", default-features = false, features = ["std", "derive"] }
serde_json = { version = "1.0.128", default-features = false, features = ["std"] }
log = { version = "0.4.22" }

vss-client = "0.3"
prost = { version = "0.11.6", default-features = false}
Expand Down
5 changes: 5 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ dictionary FilesystemLoggerConfig {
LdkLevel level;
};

dictionary LogFacadeLoggerConfig {
LdkLevel level;
};

interface Builder {
constructor();
[Name=from_config]
Expand All @@ -54,6 +58,7 @@ interface Builder {
void set_liquidity_source_lsps2(SocketAddress address, PublicKey node_id, string? token);
void set_storage_dir_path(string storage_dir_path);
void set_filesystem_logger(FilesystemLoggerConfig fs_config);
void set_log_facade_logger(LogFacadeLoggerConfig lf_config);
void set_network(Network network);
[Throws=BuildError]
void set_listening_addresses(sequence<SocketAddress> listening_addresses);
Expand Down
19 changes: 18 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
use crate::config::{
default_user_config, Config, EsploraSyncConfig, FilesystemLoggerConfig, WALLET_KEYS_SEED_LEN,
default_user_config, Config, EsploraSyncConfig, FilesystemLoggerConfig, LogFacadeLoggerConfig,
WALLET_KEYS_SEED_LEN,
};

use crate::connection::ConnectionManager;
Expand Down Expand Up @@ -111,6 +112,7 @@ impl Default for LiquiditySourceConfig {
#[derive(Debug)]
enum LogWriterConfig {
File(FilesystemLoggerConfig),
Log(LogFacadeLoggerConfig),
}

impl Default for LogWriterConfig {
Expand Down Expand Up @@ -320,6 +322,12 @@ impl NodeBuilder {
self
}

/// Configures the [`Node`] instance to write logs to the `log` facade.
pub fn set_log_facade_logger(&mut self, lf_config: LogFacadeLoggerConfig) -> &mut Self {
self.log_writer_config = Some(LogWriterConfig::Log(lf_config));
self
}

/// Sets the Bitcoin network used.
pub fn set_network(&mut self, network: Network) -> &mut Self {
self.config.network = network;
Expand Down Expand Up @@ -635,6 +643,11 @@ impl ArcedNodeBuilder {
self.inner.write().unwrap().set_filesystem_logger(fs_config);
}

/// Configures the [`Node`] instance to write logs to the `log` facade.
pub fn set_log_facade_logger(&self, lf_config: LogFacadeLoggerConfig) {
self.inner.write().unwrap().set_log_facade_logger(lf_config);
}

/// Sets the Bitcoin network used.
pub fn set_network(&self, network: Network) {
self.inner.write().unwrap().set_network(network);
Expand Down Expand Up @@ -1257,6 +1270,10 @@ fn setup_logger(config: &LogWriterConfig) -> Result<Arc<Logger>, BuildError> {
.map_err(|_| BuildError::LoggerSetupFailed)?,
))
},
LogWriterConfig::Log(log_facade_logger_config) => Ok(Arc::new(
Logger::new_log_facade(log_facade_logger_config.level)
.map_err(|_| BuildError::LoggerSetupFailed)?,
)),
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ pub struct FilesystemLoggerConfig {
pub level: LdkLevel,
}

/// Configuration options for logging to the `log` facade.
#[derive(Debug)]
pub struct LogFacadeLoggerConfig {
/// This specifies the log level.
pub level: LdkLevel,
}

impl Default for FilesystemLoggerConfig {
fn default() -> Self {
let log_file_path = format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILE_PATH);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub use event::Event;

pub use io::utils::generate_entropy_mnemonic;

pub use config::FilesystemLoggerConfig;
pub use config::{FilesystemLoggerConfig, LogFacadeLoggerConfig};
pub use logger::{LdkLevel, LogRecord, LogWriter};

#[cfg(feature = "uniffi")]
Expand Down
21 changes: 21 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) use lightning::{log_bytes, log_debug, log_error, log_info, log_trace}
pub use lightning::util::logger::Level as LdkLevel;

use chrono::Utc;
use log::{debug, error, info, trace, warn};

use std::fs;
use std::io::Write;
Expand Down Expand Up @@ -52,10 +53,16 @@ pub(crate) struct FilesystemLogger {
level: LdkLevel,
}

pub(crate) struct LogFacadeLogger {
level: LdkLevel,
}

/// Defines a writer for [`Logger`].
pub(crate) enum Writer {
/// Writes logs to the file system.
FileWriter(FilesystemLogger),
/// Fowards logs to the `log` facade.
LogFacadeWriter(LogFacadeLogger),
}

impl LogWriter for Writer {
Expand Down Expand Up @@ -84,6 +91,14 @@ impl LogWriter for Writer {
.write_all(log.as_bytes())
.expect("Failed to write to log file")
},
Writer::LogFacadeWriter(log_facade_logger) => match log_facade_logger.level {
LdkLevel::Gossip => trace!("{}", log),
LdkLevel::Trace => trace!("{}", log),
LdkLevel::Debug => debug!("{}", log),
LdkLevel::Info => info!("{}", log),
LdkLevel::Warn => warn!("{}", log),
LdkLevel::Error => error!("{}", log),
},
}
}
}
Expand Down Expand Up @@ -113,6 +128,12 @@ impl Logger {

Ok(Self { writer: Writer::FileWriter(fs_writer) })
}

pub fn new_log_facade(level: LdkLevel) -> Result<Self, ()> {
let log_facade_writer = LogFacadeLogger { level };

Ok(Self { writer: Writer::LogFacadeWriter(log_facade_writer) })
}
}

impl LdkLogger for Logger {
Expand Down

0 comments on commit 82ffcbf

Please sign in to comment.