diff --git a/config_spec.toml b/config_spec.toml index e1fa6304..366021c4 100644 --- a/config_spec.toml +++ b/config_spec.toml @@ -25,3 +25,9 @@ doc = "The path to LND macaroon file. Note: the abosolute macaroon file path is name = "log_dir" type = "String" doc = "The path to the lndk log file" + +[[param]] +name = "log_level" +type = "String" +optional = true +doc = "The log verbosity level. This can be set to either 'error', 'warn', 'info', 'debug' or 'trace'." diff --git a/src/cli.rs b/src/cli.rs index f3af5388..39fe973e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,7 @@ use clap::{Parser, Subcommand}; use lndk::lnd::{get_lnd_client, string_to_network, LndCfg}; use lndk::lndk_offers::{decode, get_destination}; use lndk::{Cfg, LifecycleSignals, LndkOnionMessenger, OfferHandler, PayOfferParams}; +use log::LevelFilter; use std::ffi::OsString; use tokio::select; use tokio::sync::mpsc; @@ -133,6 +134,7 @@ async fn main() -> Result<(), ()> { let lndk_cfg = Cfg { lnd: lnd_cfg, log_dir: None, + log_level: LevelFilter::Trace, signals, }; diff --git a/src/lib.rs b/src/lib.rs index d7ad2afb..53438177 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ use lightning::sign::{EntropySource, KeyMaterial}; use log::{error, info, LevelFilter}; use log4rs::append::console::ConsoleAppender; use log4rs::append::file::FileAppender; -use log4rs::config::{Appender, Config as LogConfig, Root}; +use log4rs::config::{Appender, Config as LogConfig, Logger, Root}; use log4rs::encode::pattern::PatternEncoder; use std::collections::HashMap; use std::str::FromStr; @@ -44,6 +44,7 @@ static INIT: Once = Once::new(); pub struct Cfg { pub lnd: LndCfg, pub log_dir: Option, + pub log_level: LevelFilter, pub signals: LifecycleSignals, } @@ -93,11 +94,16 @@ impl LndkOnionMessenger { let config = LogConfig::builder() .appender(Appender::builder().build("stdout", Box::new(stdout))) .appender(Appender::builder().build("lndk_logs", Box::new(lndk_logs))) + .logger(Logger::builder().build("h2", LevelFilter::Info)) + .logger(Logger::builder().build("hyper", LevelFilter::Info)) + .logger(Logger::builder().build("rustls", LevelFilter::Info)) + .logger(Logger::builder().build("tokio_util", LevelFilter::Info)) + .logger(Logger::builder().build("tracing", LevelFilter::Info)) .build( Root::builder() .appender("stdout") .appender("lndk_logs") - .build(LevelFilter::Info), + .build(args.log_level), ) .unwrap(); diff --git a/src/main.rs b/src/main.rs index a98fb46e..a1c6e836 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,8 @@ mod internal { use internal::*; use lndk::lnd::LndCfg; use lndk::{Cfg, LifecycleSignals, LndkOnionMessenger, OfferHandler}; +use log::{error, LevelFilter}; +use std::str::FromStr; use tokio::sync::mpsc; use tokio::sync::mpsc::{Receiver, Sender}; @@ -25,6 +27,20 @@ async fn main() -> Result<(), ()> { .0; let lnd_args = LndCfg::new(config.address, config.cert, config.macaroon); + let log_level = match config.log_level { + Some(level_str) => match LevelFilter::from_str(&level_str) { + Ok(level) => level, + Err(_) => { + error!( + "User provided log level '{}' is invalid. Make sure it is set to either 'error', + 'warn', 'info', 'debug' or 'trace'", + level_str + ); + return Err(()); + } + }, + None => LevelFilter::Info, + }; let (shutdown, listener) = triggered::trigger(); // Create the channel which will tell us when the onion messenger has finished starting up. let (tx, _): (Sender, Receiver) = mpsc::channel(1); @@ -36,6 +52,7 @@ async fn main() -> Result<(), ()> { let args = Cfg { lnd: lnd_args, log_dir: config.log_dir, + log_level, signals, }; diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index a9a921ca..a55e3180 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -12,6 +12,7 @@ use lightning::offers::offer::Quantity; use lightning::onion_message::messenger::Destination; use lndk::onion_messenger::MessengerUtilities; use lndk::{LifecycleSignals, PayOfferParams}; +use log::LevelFilter; use std::net::SocketAddr; use std::path::PathBuf; use std::str::FromStr; @@ -93,6 +94,7 @@ async fn test_lndk_forwards_onion_message() { .unwrap() .to_string(), ), + log_level: LevelFilter::Info, signals, }; @@ -205,6 +207,7 @@ async fn test_lndk_send_invoice_request() { .unwrap() .to_string(), ), + log_level: LevelFilter::Info, signals, }; @@ -279,6 +282,7 @@ async fn test_lndk_send_invoice_request() { .unwrap() .to_string(), ), + log_level: LevelFilter::Info, signals, }; @@ -407,6 +411,7 @@ async fn test_lndk_pay_offer() { .unwrap() .to_string(), ), + log_level: LevelFilter::Info, signals, };