Skip to content

Commit

Permalink
Improve log level user interface (#539)
Browse files Browse the repository at this point in the history
* Configure env_logger manually

* Set log verbosity through config API, scope it to aquadoggo by default

* Add entry to CHANGELOG.md
  • Loading branch information
adzialocha authored Aug 30, 2023
1 parent 0cdfa63 commit 9c8c49e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rework networking service [#502](https://github.com/p2panda/aquadoggo/pull/502)
- Deduplicate peer connections when initiating replication sessions [#525](https://github.com/p2panda/aquadoggo/pull/525)
- Improve consistency and documentation of configuration API [#528](https://github.com/p2panda/aquadoggo/pull/528)
- Improve log level config and user interface [#539](https://github.com/p2panda/aquadoggo/pull/539)

### Fixed

Expand Down
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
14 changes: 11 additions & 3 deletions aquadoggo_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ mod key_pair;
mod utils;

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

use anyhow::Context;
use aquadoggo::{AllowList, Configuration, Node};
use log::warn;
use env_logger::WriteStyle;
use log::{warn, LevelFilter};

use crate::config::{load_config, print_config};
use crate::key_pair::{generate_ephemeral_key_pair, generate_or_load_key_pair};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

// Load configuration from command line arguments, environment variables and .toml file
let (config_file_path, config) = load_config().context("Could not load configuration")?;

// Set log verbosity based on config. By default scope it always to the "aquadoggo" module.
let mut builder = env_logger::Builder::new();
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
.clone()
Expand Down

0 comments on commit 9c8c49e

Please sign in to comment.