From b6d8a6ec6aeaeeffdf3fb4f325f283e8974f6966 Mon Sep 17 00:00:00 2001 From: YoussefAWasfy <112941926+YoussefAWasfy@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:15:43 +0200 Subject: [PATCH] fix: Improve logger (#45) * feat: local mediator * feat: use conf to override env logging level * feat: u * change log level to info --- .../conf/mediator.toml | 2 +- affinidi-messaging-mediator/src/lib.rs | 23 ++++--------------- affinidi-messaging-mediator/src/server.rs | 4 ++-- .../src/tasks/statistics.rs | 2 +- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/affinidi-messaging-mediator/conf/mediator.toml b/affinidi-messaging-mediator/conf/mediator.toml index 6f09ef5..83b0bc4 100644 --- a/affinidi-messaging-mediator/conf/mediator.toml +++ b/affinidi-messaging-mediator/conf/mediator.toml @@ -1,6 +1,6 @@ ### log_level: trace debug info warn error ### default: info -log_level = "info" +log_level = "info,affinidi_messaging_mediator=info,affinidi_did_resolver_cache_sdk=debug,affinidi_messaging_sdk=debug,tower_http=trace" ### listen_address: : that this service will listen on. ### Default: 0.0.0.0:7037 diff --git a/affinidi-messaging-mediator/src/lib.rs b/affinidi-messaging-mediator/src/lib.rs index 92d7910..d7e592f 100644 --- a/affinidi-messaging-mediator/src/lib.rs +++ b/affinidi-messaging-mediator/src/lib.rs @@ -14,8 +14,8 @@ use common::{ use database::DatabaseHandler; use http::request::Parts; use tasks::websocket_streaming::StreamingTask; -use tracing::{event, level_filters::LevelFilter, Level}; -use tracing_subscriber::{reload::Handle, Registry}; +use tracing::{event, Level}; +use tracing_subscriber::{reload::Handle, EnvFilter, Registry}; pub mod common; pub mod database; @@ -57,33 +57,20 @@ where } pub async fn init( - reload_handle: Option>, + reload_handle: Option>, ) -> Result { // Read configuration file parameters let config = read_config_file("conf/mediator.toml")?; // Setup logging if reload_handle.is_some() { - let level: LevelFilter = match config.log_level.as_str() { - "trace" => LevelFilter::TRACE, - "debug" => LevelFilter::DEBUG, - "info" => LevelFilter::INFO, - "warn" => LevelFilter::WARN, - "error" => LevelFilter::ERROR, - _ => { - event!( - Level::WARN, - "log_level({}) is unknown in config file. Defaults to INFO", - config.log_level - ); - LevelFilter::INFO - } - }; + let level: EnvFilter = EnvFilter::new(config.log_level.as_str()); reload_handle .unwrap() .modify(|filter| *filter = level) .map_err(|e| MediatorError::InternalError("NA".into(), e.to_string()))?; event!(Level::INFO, "Log level set to ({})", config.log_level); + event!(Level::DEBUG, "Log level set to ({})", config.log_level); } match >::try_from(config).await { diff --git a/affinidi-messaging-mediator/src/server.rs b/affinidi-messaging-mediator/src/server.rs index aa14db9..76616fd 100644 --- a/affinidi-messaging-mediator/src/server.rs +++ b/affinidi-messaging-mediator/src/server.rs @@ -14,11 +14,11 @@ use std::{env, net::SocketAddr}; use tower_http::limit::RequestBodyLimitLayer; use tower_http::trace::{self, TraceLayer}; use tracing::{event, Level}; -use tracing_subscriber::{filter, layer::SubscriberExt, reload, util::SubscriberInitExt}; +use tracing_subscriber::{layer::SubscriberExt, reload, util::SubscriberInitExt, EnvFilter}; pub async fn start() { // setup logging/tracing framework - let filter = filter::LevelFilter::INFO; // This can be changed in the config file! + let filter = EnvFilter::from_default_env(); // This can be changed in the config file! let (filter, reload_handle) = reload::Layer::new(filter); let ansi = env::var("LOCAL").is_ok(); tracing_subscriber::registry() diff --git a/affinidi-messaging-mediator/src/tasks/statistics.rs b/affinidi-messaging-mediator/src/tasks/statistics.rs index cf18d65..cbd8e63 100644 --- a/affinidi-messaging-mediator/src/tasks/statistics.rs +++ b/affinidi-messaging-mediator/src/tasks/statistics.rs @@ -22,7 +22,7 @@ pub async fn statistics(database: DatabaseHandler) -> Result<(), MediatorError> interval.tick().await; let stats = database.get_db_metadata().await?; info!("Statistics: {}", stats); - info!("Delta: {}", stats.delta(&previous_stats)); + info!("Delta: {:?}", stats.delta(&previous_stats)); previous_stats = stats; }