Skip to content

Commit 025bf9b

Browse files
committed
Allow configuring the log level, default to Debug
1 parent e8f8db3 commit 025bf9b

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

bindings/ldk_node.udl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dictionary Config {
77
Network network;
88
NetAddress? listening_address;
99
u32 default_cltv_expiry_delta;
10+
LogLevel log_level;
1011
};
1112

1213
interface Builder {
@@ -156,6 +157,15 @@ dictionary PeerDetails {
156157
boolean is_connected;
157158
};
158159

160+
enum LogLevel {
161+
"Gossip",
162+
"Trace",
163+
"Debug",
164+
"Info",
165+
"Warn",
166+
"Error",
167+
};
168+
159169
[Custom]
160170
typedef string Txid;
161171

src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ mod wallet;
9090
pub use bip39;
9191
pub use bitcoin;
9292
pub use lightning;
93-
use lightning::ln::msgs::RoutingMessageHandler;
9493
pub use lightning_invoice;
9594

9695
pub use error::Error as NodeError;
@@ -120,11 +119,13 @@ use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch};
120119
use lightning::ln::channelmanager::{
121120
self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, Retry,
122121
};
122+
use lightning::ln::msgs::RoutingMessageHandler;
123123
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
124124
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
125125
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
126126

127127
use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
128+
pub use lightning::util::logger::Level as LogLevel;
128129
use lightning::util::ser::ReadableArgs;
129130

130131
use lightning_background_processor::process_events_async;
@@ -194,6 +195,10 @@ pub struct Config {
194195
pub listening_address: Option<NetAddress>,
195196
/// The default CLTV expiry delta to be used for payments.
196197
pub default_cltv_expiry_delta: u32,
198+
/// The level at which we log messages.
199+
///
200+
/// Any messages below this level will be excluded from the logs.
201+
pub log_level: LogLevel,
197202
}
198203

199204
impl Default for Config {
@@ -204,6 +209,7 @@ impl Default for Config {
204209
network: Network::Regtest,
205210
listening_address: Some("0.0.0.0:9735".parse().unwrap()),
206211
default_cltv_expiry_delta: 144,
212+
log_level: LogLevel::Debug,
207213
}
208214
}
209215
}
@@ -320,6 +326,12 @@ impl Builder {
320326
self
321327
}
322328

329+
/// Sets the level at which [`Node`] will log messages.
330+
pub fn set_log_level(&mut self, level: LogLevel) -> &mut Self {
331+
self.config.log_level = level;
332+
self
333+
}
334+
323335
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
324336
/// previously configured.
325337
pub fn build(&self) -> Arc<Node<FilesystemStore>> {
@@ -342,7 +354,7 @@ impl Builder {
342354

343355
// Initialize the Logger
344356
let log_file_path = format!("{}/ldk_node.log", config.storage_dir_path);
345-
let logger = Arc::new(FilesystemLogger::new(log_file_path));
357+
let logger = Arc::new(FilesystemLogger::new(log_file_path, config.log_level));
346358

347359
// Initialize the on-chain wallet and chain access
348360
let seed_bytes = if let Some(entropy_source_config) = &self.entropy_source_config {

src/logger.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
pub(crate) use lightning::util::logger::Logger;
2-
use lightning::util::logger::Record;
3-
use lightning::util::ser::Writer;
42
pub(crate) use lightning::{log_error, log_info, log_trace};
53

4+
use lightning::util::logger::{Level, Record};
5+
use lightning::util::ser::Writer;
6+
67
use chrono::Utc;
78

89
use std::fs;
910
use std::path::Path;
1011

1112
pub(crate) struct FilesystemLogger {
1213
file_path: String,
14+
level: Level,
1315
}
1416

1517
impl FilesystemLogger {
16-
pub(crate) fn new(file_path: String) -> Self {
18+
pub(crate) fn new(file_path: String, level: Level) -> Self {
1719
if let Some(parent_dir) = Path::new(&file_path).parent() {
1820
fs::create_dir_all(parent_dir).unwrap();
1921
}
20-
Self { file_path }
22+
Self { file_path, level }
2123
}
2224
}
2325
impl Logger for FilesystemLogger {
2426
fn log(&self, record: &Record) {
27+
if record.level < self.level {
28+
return;
29+
}
2530
let raw_log = record.args.to_string();
2631
let log = format!(
2732
"{} {:<5} [{}:{}] {}\n",

src/test/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ pub fn random_config(esplora_url: &str) -> Config {
266266
let listening_address_str = format!("127.0.0.1:{}", rand_port);
267267
config.listening_address = Some(listening_address_str.parse().unwrap());
268268

269+
config.log_level = Level::Trace;
270+
269271
config
270272
}
271273

0 commit comments

Comments
 (0)