Skip to content

Commit

Permalink
Merge pull request #96 from lndk-org/log-tweaks
Browse files Browse the repository at this point in the history
Logging fixes
  • Loading branch information
carlaKC authored Mar 16, 2024
2 parents 00a2d13 + 36a1d00 commit 088272e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'."
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,6 +134,7 @@ async fn main() -> Result<(), ()> {
let lndk_cfg = Cfg {
lnd: lnd_cfg,
log_dir: None,
log_level: LevelFilter::Trace,
signals,
};

Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +44,7 @@ static INIT: Once = Once::new();
pub struct Cfg {
pub lnd: LndCfg,
pub log_dir: Option<String>,
pub log_level: LevelFilter,
pub signals: LifecycleSignals,
}

Expand Down Expand Up @@ -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();

Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<u32>, Receiver<u32>) = mpsc::channel(1);
Expand All @@ -36,6 +52,7 @@ async fn main() -> Result<(), ()> {
let args = Cfg {
lnd: lnd_args,
log_dir: config.log_dir,
log_level,
signals,
};

Expand Down
5 changes: 5 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,6 +94,7 @@ async fn test_lndk_forwards_onion_message() {
.unwrap()
.to_string(),
),
log_level: LevelFilter::Info,
signals,
};

Expand Down Expand Up @@ -205,6 +207,7 @@ async fn test_lndk_send_invoice_request() {
.unwrap()
.to_string(),
),
log_level: LevelFilter::Info,
signals,
};

Expand Down Expand Up @@ -279,6 +282,7 @@ async fn test_lndk_send_invoice_request() {
.unwrap()
.to_string(),
),
log_level: LevelFilter::Info,
signals,
};

Expand Down Expand Up @@ -407,6 +411,7 @@ async fn test_lndk_pay_offer() {
.unwrap()
.to_string(),
),
log_level: LevelFilter::Info,
signals,
};

Expand Down

0 comments on commit 088272e

Please sign in to comment.