Skip to content

Commit

Permalink
Set log verbosity through config API, scope it to aquadoggo by default
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed Aug 30, 2023
1 parent 17a7450 commit 1585131
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 13 additions & 0 deletions aquadoggo_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ struct Cli {
)]
#[serde(skip_serializing_if = "Option::is_none")]
relay_mode: Option<bool>,

/// Set log verbosity. Use this for learning more about how your node behaves or for debugging.
///
/// Possible log levels are: ERROR, WARN, INFO, DEBUG, TRACE. They are scoped to "aquadoggo" by
/// default.
///
/// If you want to adjust the scope for deeper inspection use a filter value, for example
/// "=TRACE" for logging _everything_ or "aquadoggo=INFO,libp2p=DEBUG" etc.
#[arg(short = 'l', long, value_name = "LEVEL")]
#[serde(skip_serializing_if = "Option::is_none")]
log_level: Option<String>,
}

/// Clap converts wildcard symbols from command line arguments (for example --supported-schema-ids
Expand Down Expand Up @@ -250,6 +261,7 @@ where
/// Configuration derived from environment variables and .toml file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Configuration {
pub log_level: String,
pub allow_schema_ids: UncheckedAllowList,
pub database_url: String,
pub database_max_connections: u32,
Expand All @@ -268,6 +280,7 @@ pub struct Configuration {
impl Default for Configuration {
fn default() -> Self {
Self {
log_level: "off".into(),
allow_schema_ids: UncheckedAllowList::Wildcard,
database_url: "sqlite::memory:".into(),
database_max_connections: 32,
Expand Down
12 changes: 7 additions & 5 deletions aquadoggo_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod key_pair;
mod utils;

use std::convert::TryInto;
use std::str::FromStr;

use anyhow::Context;
use aquadoggo::{AllowList, Configuration, Node};
Expand All @@ -19,12 +20,13 @@ async fn main() -> anyhow::Result<()> {
// Load configuration from command line arguments, environment variables and .toml file
let (config_file_path, config) = load_config().context("Could not load configuration")?;

// Configure log level
// Set log verbosity based on config. By default scope it always to the "aquadoggo" module.
let mut builder = env_logger::Builder::new();
builder
.filter(Some("aquadoggo"), LevelFilter::Info)
.write_style(WriteStyle::Always)
.init();
let builder = match LevelFilter::from_str(&config.log_level) {
Ok(log_level) => builder.filter(Some("aquadoggo"), log_level),
Err(_) => builder.parse_filters(&config.log_level),
};
builder.write_style(WriteStyle::Always).init();

// Convert to `aquadoggo` configuration format and check for invalid inputs
let node_config = config
Expand Down

0 comments on commit 1585131

Please sign in to comment.