Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve log level user interface #539

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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