Skip to content

Commit

Permalink
feat: improve log level control
Browse files Browse the repository at this point in the history
- Adjust method instrumentation span to debug verbosity
- Separate service log verbosity from RUST_LOG so dependency logging and
service logging can be adjusted independently
  • Loading branch information
Tim Holmes-Mitra committed Jan 24, 2024
1 parent 4a79a72 commit 62c4563
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
30 changes: 29 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion src/features/app/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetRatingRequest>,
Expand Down
10 changes: 5 additions & 5 deletions src/features/user/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthenticateRequest>,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl User for UserService {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "debug")]
async fn delete(&self, request: Request<()>) -> Result<Response<()>, Status> {
let app_ctx = request.extensions().get::<AppContext>().unwrap().clone();
let Claims {
Expand All @@ -56,7 +56,7 @@ impl User for UserService {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "debug")]
async fn vote(&self, request: Request<VoteRequest>) -> Result<Response<()>, Status> {
let app_ctx = request.extensions().get::<AppContext>().unwrap().clone();
let Claims {
Expand All @@ -78,7 +78,7 @@ impl User for UserService {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "debug")]
async fn list_my_votes(
&self,
request: Request<ListMyVotesRequest>,
Expand All @@ -105,7 +105,7 @@ impl User for UserService {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "debug")]
async fn get_snap_votes(
&self,
request: Request<GetSnapVotesRequest>,
Expand Down
16 changes: 7 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tracing_subscriber::EnvFilter;

mod app;
mod features;
mod utils;
Expand All @@ -6,21 +8,17 @@ mod utils;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
Expand Down

0 comments on commit 62c4563

Please sign in to comment.