From 98b8d412e730ef29e3bf155e9d7b00664f8c816c34f8d718fef2cd06f9a89f66 Mon Sep 17 00:00:00 2001 From: raven <7156279+RavenX8@users.noreply.github.com> Date: Wed, 19 Mar 2025 02:13:02 -0400 Subject: [PATCH] - update: logging system to only show logs from the app and not any 3rd party dependencies --- api-service/src/main.rs | 10 ++++------ auth-service/Cargo.toml | 2 +- auth-service/src/main.rs | 20 +++++++------------- character-service/Cargo.toml | 2 +- character-service/src/main.rs | 10 ++++------ database-service/Cargo.toml | 2 +- database-service/src/main.rs | 10 ++++------ packet-service/Cargo.toml | 2 +- packet-service/src/main.rs | 11 ++++------- session-service/Cargo.toml | 2 +- session-service/src/main.rs | 11 ++++------- utils/Cargo.toml | 1 + utils/src/lib.rs | 1 + utils/src/logging.rs | 19 +++++++++++++++++++ world-service/Cargo.toml | 2 +- world-service/src/main.rs | 13 +++---------- 16 files changed, 57 insertions(+), 61 deletions(-) create mode 100644 utils/src/logging.rs diff --git a/api-service/src/main.rs b/api-service/src/main.rs index c10aaa4..1887613 100644 --- a/api-service/src/main.rs +++ b/api-service/src/main.rs @@ -6,7 +6,9 @@ use std::str::FromStr; use std::sync::Arc; use tokio::sync::Mutex; use tracing::{info, Level}; +use tracing_subscriber::EnvFilter; use utils::consul_registration; +use utils::logging; use utils::service_discovery::{get_kube_service_endpoints_by_dns}; mod axum_gateway; @@ -14,12 +16,8 @@ mod axum_gateway; #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); diff --git a/auth-service/Cargo.toml b/auth-service/Cargo.toml index 722006e..97ffc30 100644 --- a/auth-service/Cargo.toml +++ b/auth-service/Cargo.toml @@ -15,7 +15,7 @@ argon2 = "0.5.3" serde = { version = "1.0", features = ["derive"] } dotenv = "0.15" tracing = "0.1" -tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } prost = "0.13.4" prost-types = "0.13.3" chrono = { version = "0.4.38", features = ["serde"] } diff --git a/auth-service/src/main.rs b/auth-service/src/main.rs index 11d0511..88ccad8 100644 --- a/auth-service/src/main.rs +++ b/auth-service/src/main.rs @@ -4,14 +4,12 @@ use auth_service::database_client::DatabaseClientTrait; use auth_service::grpc::MyAuthService; use auth_service::session::session_service_client::SessionServiceClient; use dotenv::dotenv; -use std::collections::HashMap; use std::env; -use std::str::FromStr; use std::sync::Arc; use tonic::transport::Server; -use tracing::{debug, info, Level}; -use utils::consul_registration; -use utils::multi_service_load_balancer::{LoadBalancingStrategy, MultiServiceLoadBalancer}; +use tracing::info; +use tracing_subscriber::{fmt, EnvFilter}; +use utils::logging; use utils::service_discovery::{get_kube_service_endpoints_by_dns}; #[tokio::main] @@ -19,13 +17,9 @@ async fn main() -> Result<(), Box> { // Load environment variables from .env dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); - + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); + // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "50051".to_string()); @@ -45,7 +39,7 @@ async fn main() -> Result<(), Box> { let (mut health_reporter, health_service) = tonic_health::server::health_reporter(); health_reporter.set_serving::>().await; - println!("Authentication Service running on {}", addr); + info!("Authentication Service running on {}", addr); // Start the gRPC server tokio::spawn( diff --git a/character-service/Cargo.toml b/character-service/Cargo.toml index 8c063c5..a19b863 100644 --- a/character-service/Cargo.toml +++ b/character-service/Cargo.toml @@ -9,7 +9,7 @@ dotenv = "0.15" tokio = { version = "1.41.1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } tracing = "0.1" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } tonic = "0.12.3" prost = "0.13.4" warp = "0.3.7" diff --git a/character-service/src/main.rs b/character-service/src/main.rs index 39f43d7..b2041c2 100644 --- a/character-service/src/main.rs +++ b/character-service/src/main.rs @@ -12,17 +12,15 @@ use std::env; use std::str::FromStr; use std::sync::Arc; use tracing::Level; +use tracing_subscriber::EnvFilter; +use utils::logging; use utils::service_discovery::{get_kube_service_endpoints_by_dns}; #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); diff --git a/database-service/Cargo.toml b/database-service/Cargo.toml index 1da6f24..6b84728 100644 --- a/database-service/Cargo.toml +++ b/database-service/Cargo.toml @@ -15,7 +15,7 @@ chrono = { version = "0.4.39", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } dotenv = "0.15" tracing = "0.1" -tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } prost = "0.13.3" serde_json = "1.0.133" async-trait = "0.1.83" diff --git a/database-service/src/main.rs b/database-service/src/main.rs index 2cd687c..4387b5b 100644 --- a/database-service/src/main.rs +++ b/database-service/src/main.rs @@ -11,17 +11,15 @@ use std::sync::Arc; use tokio::sync::Mutex; use tonic::transport::Server; use tracing::{info, Level}; +use tracing_subscriber::EnvFilter; +use utils::logging; use utils::redis_cache::RedisCache; #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "50052".to_string()); diff --git a/packet-service/Cargo.toml b/packet-service/Cargo.toml index b829ddd..6c9c111 100644 --- a/packet-service/Cargo.toml +++ b/packet-service/Cargo.toml @@ -13,7 +13,7 @@ tokio = { version = "1.41.1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } bytes = { version = "1.8.0", features = ["std", "serde"] } tracing = "0.1" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } bincode = { version = "2.0.0", features = ["derive", "serde"] } thiserror = "2.0.3" lazy_static = "1.5.0" diff --git a/packet-service/src/main.rs b/packet-service/src/main.rs index 07eef92..baa8a78 100644 --- a/packet-service/src/main.rs +++ b/packet-service/src/main.rs @@ -20,7 +20,8 @@ use tokio::sync::{Mutex, Semaphore}; use tokio::{select, signal}; use tracing::Level; use tracing::{debug, error, info, warn}; -use utils::consul_registration; +use tracing_subscriber::EnvFilter; +use utils::{consul_registration, logging}; use utils::service_discovery::{get_kube_service_endpoints_by_dns}; use warp::Filter; @@ -58,12 +59,8 @@ const MAX_CONCURRENT_CONNECTIONS: usize = 100; #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); diff --git a/session-service/Cargo.toml b/session-service/Cargo.toml index 4a72afa..0d0c113 100644 --- a/session-service/Cargo.toml +++ b/session-service/Cargo.toml @@ -9,7 +9,7 @@ dotenv = "0.15" tokio = { version = "1.41.1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } tracing = "0.1" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } tonic = "0.12.3" prost = "0.13.4" warp = "0.3.7" diff --git a/session-service/src/main.rs b/session-service/src/main.rs index e5228af..1d451b4 100644 --- a/session-service/src/main.rs +++ b/session-service/src/main.rs @@ -10,7 +10,8 @@ use std::sync::Arc; use tokio::sync::Mutex; use tonic::transport::Server; use tracing::Level; -use utils::consul_registration; +use tracing_subscriber::{fmt, EnvFilter}; +use utils::{consul_registration, logging}; use utils::redis_cache::RedisCache; pub mod common { @@ -24,12 +25,8 @@ pub mod api { #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 56acc24..5b39e48 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -19,3 +19,4 @@ hickory-resolver = "0.24.4" rand = "0.8.5" kube = { version = "0.99.0", features = ["derive"] } k8s-openapi = { version = "0.24.0", features = ["v1_32"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 06e1115..5d375c2 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -4,3 +4,4 @@ pub mod redis_cache; pub mod service_discovery; pub mod signal_handler; pub mod multi_service_load_balancer; +pub mod logging; diff --git a/utils/src/logging.rs b/utils/src/logging.rs new file mode 100644 index 0000000..49911a1 --- /dev/null +++ b/utils/src/logging.rs @@ -0,0 +1,19 @@ +use std::env; +use tracing::{debug, error, info, trace, warn}; +use tracing_subscriber::EnvFilter; + +pub fn setup_logging(app_name: &str) { + let log_level = env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()); + let filter = EnvFilter::try_new(format!("{app_name}={log_level},utils={log_level}")) + .unwrap_or_else(|_| EnvFilter::new(format!("{app_name}=info,utils=info"))); + + tracing_subscriber::fmt() + .with_env_filter(filter) + .init(); + + error!("Error messages enabled"); + warn!("Warning messages enabled"); + info!("Info messages enabled"); + debug!("Debug messages enabled"); + trace!("Trace messages enabled"); +} \ No newline at end of file diff --git a/world-service/Cargo.toml b/world-service/Cargo.toml index ae131d1..9b5c615 100644 --- a/world-service/Cargo.toml +++ b/world-service/Cargo.toml @@ -9,7 +9,7 @@ dotenv = "0.15" tokio = { version = "1.41.1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } tracing = "0.1" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] } tonic = "0.12.3" prost = "0.13.4" warp = "0.3.7" diff --git a/world-service/src/main.rs b/world-service/src/main.rs index 7359b4a..0d7a054 100644 --- a/world-service/src/main.rs +++ b/world-service/src/main.rs @@ -1,20 +1,13 @@ use dotenv::dotenv; -use std::collections::HashMap; use std::env; -use std::str::FromStr; -use tracing::{debug, Level}; -use utils::consul_registration; +use utils::{consul_registration, logging}; use utils::service_discovery::{get_kube_service_endpoints_by_dns, get_service_endpoints_by_dns}; #[tokio::main] async fn main() -> Result<(), Box> { dotenv().ok(); - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); + let app_name = env!("CARGO_PKG_NAME"); + logging::setup_logging(app_name); // Set the gRPC server address let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());