Skip to content

Commit 8a7a507

Browse files
committed
Allow configuring the log level, default to Debug
1 parent 32701ef commit 8a7a507

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

bindings/ldk_node.udl

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

1112
interface Builder {
@@ -172,6 +173,15 @@ dictionary PeerDetails {
172173
boolean is_connected;
173174
};
174175

176+
enum LogLevel {
177+
"Gossip",
178+
"Trace",
179+
"Debug",
180+
"Info",
181+
"Warn",
182+
"Error",
183+
};
184+
175185
[Custom]
176186
typedef string Txid;
177187

src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ mod wallet;
9393
pub use bip39;
9494
pub use bitcoin;
9595
pub use lightning;
96-
use lightning::ln::msgs::RoutingMessageHandler;
9796
pub use lightning_invoice;
9897

9998
pub use error::Error as NodeError;
@@ -126,11 +125,13 @@ use lightning::chain::{chainmonitor, BestBlock, Confirm, Watch};
126125
use lightning::ln::channelmanager::{
127126
self, ChainParameters, ChannelManagerReadArgs, PaymentId, RecipientOnionFields, Retry,
128127
};
128+
use lightning::ln::msgs::RoutingMessageHandler;
129129
use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler};
130130
use lightning::ln::{PaymentHash, PaymentPreimage};
131131
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
132132

133133
use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig};
134+
pub use lightning::util::logger::Level as LogLevel;
134135
use lightning::util::ser::ReadableArgs;
135136

136137
use lightning_background_processor::process_events_async;
@@ -173,6 +174,7 @@ const DEFAULT_NETWORK: Network = Network::Bitcoin;
173174
const DEFAULT_LISTENING_ADDR: &str = "0.0.0.0:9735";
174175
const DEFAULT_CLTV_EXPIRY_DELTA: u32 = 144;
175176
const DEFAULT_ESPLORA_SERVER_URL: &str = "https://blockstream.info/api";
177+
const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
176178

177179
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
178180
// number of blocks after which BDK stops looking for scripts belonging to the wallet.
@@ -207,6 +209,7 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
207209
/// | `network` | Network::Bitcoin |
208210
/// | `listening_address` | 0.0.0.0:9735 |
209211
/// | `default_cltv_expiry_delta` | 144 |
212+
/// | `log_level` | `Debug` |
210213
///
211214
pub struct Config {
212215
/// The path where the underlying LDK and BDK persist their data.
@@ -217,6 +220,10 @@ pub struct Config {
217220
pub listening_address: Option<NetAddress>,
218221
/// The default CLTV expiry delta to be used for payments.
219222
pub default_cltv_expiry_delta: u32,
223+
/// The level at which we log messages.
224+
///
225+
/// Any messages below this level will be excluded from the logs.
226+
pub log_level: LogLevel,
220227
}
221228

222229
impl Default for Config {
@@ -226,6 +233,7 @@ impl Default for Config {
226233
network: DEFAULT_NETWORK,
227234
listening_address: Some(DEFAULT_LISTENING_ADDR.parse().unwrap()),
228235
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
236+
log_level: DEFAULT_LOG_LEVEL,
229237
}
230238
}
231239
}
@@ -348,6 +356,12 @@ impl Builder {
348356
config.listening_address = Some(listening_address);
349357
}
350358

359+
/// Sets the level at which [`Node`] will log messages.
360+
pub fn set_log_level(&self, level: LogLevel) {
361+
let mut config = self.config.write().unwrap();
362+
config.log_level = level;
363+
}
364+
351365
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
352366
/// previously configured.
353367
pub fn build(&self) -> Arc<Node<FilesystemStore>> {
@@ -371,7 +385,7 @@ impl Builder {
371385

372386
// Initialize the Logger
373387
let log_file_path = format!("{}/ldk_node.log", config.storage_dir_path);
374-
let logger = Arc::new(FilesystemLogger::new(log_file_path));
388+
let logger = Arc::new(FilesystemLogger::new(log_file_path, config.log_level));
375389

376390
// Initialize the on-chain wallet and chain access
377391
let seed_bytes = match &*self.entropy_source_config.read().unwrap() {

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() -> 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)