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

fix: Improve logger #45

Merged
merged 4 commits into from
Sep 24, 2024
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
2 changes: 1 addition & 1 deletion affinidi-messaging-mediator/conf/mediator.toml
Original file line number Diff line number Diff line change
@@ -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: <ip_address>:<tcp_port> that this service will listen on.
### Default: 0.0.0.0:7037
Expand Down
23 changes: 5 additions & 18 deletions affinidi-messaging-mediator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,33 +57,20 @@ where
}

pub async fn init(
reload_handle: Option<Handle<LevelFilter, Registry>>,
reload_handle: Option<Handle<EnvFilter, Registry>>,
) -> Result<Config, MediatorError> {
// 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 <common::config::Config as async_convert::TryFrom<ConfigRaw>>::try_from(config).await {
Expand Down
4 changes: 2 additions & 2 deletions affinidi-messaging-mediator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion affinidi-messaging-mediator/src/tasks/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading