From 62c4563a3ff71be7f25f1fd53cae22f6bab939d2 Mon Sep 17 00:00:00 2001 From: Tim Holmes-Mitra Date: Tue, 23 Jan 2024 16:37:37 +0000 Subject: [PATCH] feat: improve log level control - Adjust method instrumentation span to debug verbosity - Separate service log verbosity from RUST_LOG so dependency logging and service logging can be adjusted independently --- Cargo.lock | 30 +++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/features/app/interface.rs | 2 +- src/features/user/interface.rs | 10 +++++----- src/main.rs | 16 +++++++--------- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2082efea..bc15a7c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -945,6 +945,15 @@ version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata", +] + [[package]] name = "matchit" version = "0.7.0" @@ -1525,9 +1534,24 @@ version = "1.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" dependencies = [ - "regex-syntax", + "regex-syntax 0.7.2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.7.2" @@ -2509,10 +2533,14 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", ] diff --git a/Cargo.toml b/Cargo.toml index 265cd48f..c18aa3e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ tonic = "0.10.2" tonic-reflection = "0.10.2" tower = "0.4.13" tracing = "0.1.40" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } [build-dependencies] tonic-build = { version = "0.10.2", features = ["prost"] } diff --git a/src/features/app/interface.rs b/src/features/app/interface.rs index 2c568f1d..3030621c 100644 --- a/src/features/app/interface.rs +++ b/src/features/app/interface.rs @@ -8,7 +8,7 @@ use tonic::{Request, Response, Status}; #[tonic::async_trait] impl App for AppService { - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn get_rating( &self, request: Request, diff --git a/src/features/user/interface.rs b/src/features/user/interface.rs index d9469569..7c52a2cb 100644 --- a/src/features/user/interface.rs +++ b/src/features/user/interface.rs @@ -15,7 +15,7 @@ use crate::{ #[tonic::async_trait] impl User for UserService { - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn authenticate( &self, request: Request, @@ -43,7 +43,7 @@ impl User for UserService { } } - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn delete(&self, request: Request<()>) -> Result, Status> { let app_ctx = request.extensions().get::().unwrap().clone(); let Claims { @@ -56,7 +56,7 @@ impl User for UserService { } } - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn vote(&self, request: Request) -> Result, Status> { let app_ctx = request.extensions().get::().unwrap().clone(); let Claims { @@ -78,7 +78,7 @@ impl User for UserService { } } - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn list_my_votes( &self, request: Request, @@ -105,7 +105,7 @@ impl User for UserService { } } - #[tracing::instrument] + #[tracing::instrument(level = "debug")] async fn get_snap_votes( &self, request: Request, diff --git a/src/main.rs b/src/main.rs index a144f02f..1a7c2d07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use tracing_subscriber::EnvFilter; + mod app; mod features; mod utils; @@ -6,21 +8,17 @@ mod utils; async fn main() -> Result<(), Box> { let config = utils::Config::load()?; - let log_level = match config.log_level.as_str() { - "debug" => tracing::Level::DEBUG, - "info" => tracing::Level::INFO, - "warn" => tracing::Level::WARN, - "error" => tracing::Level::ERROR, - _ => tracing::Level::INFO, - }; + let app_name = config.name.as_str(); + let app_log_level = config.log_level.as_str(); + let app_logging_directive = format!("{app_name}={app_log_level}").parse()?; + let max_level = EnvFilter::from_default_env().add_directive(app_logging_directive); tracing_subscriber::fmt() - .with_max_level(log_level) + .with_env_filter(max_level) .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE) .init(); tracing::info!("Starting the Ubuntu App Rating Service"); - app::run(config).await?; Ok(())