- update: logging system to only show logs from the app and not any 3rd party dependencies

This commit is contained in:
2025-03-19 02:13:02 -04:00
parent 03cef8e526
commit 98b8d412e7
16 changed files with 57 additions and 61 deletions

View File

@@ -6,7 +6,9 @@ use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::{info, Level}; use tracing::{info, Level};
use tracing_subscriber::EnvFilter;
use utils::consul_registration; use utils::consul_registration;
use utils::logging;
use utils::service_discovery::{get_kube_service_endpoints_by_dns}; use utils::service_discovery::{get_kube_service_endpoints_by_dns};
mod axum_gateway; mod axum_gateway;
@@ -14,12 +16,8 @@ mod axum_gateway;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());

View File

@@ -15,7 +15,7 @@ argon2 = "0.5.3"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
dotenv = "0.15" dotenv = "0.15"
tracing = "0.1" 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 = "0.13.4"
prost-types = "0.13.3" prost-types = "0.13.3"
chrono = { version = "0.4.38", features = ["serde"] } chrono = { version = "0.4.38", features = ["serde"] }

View File

@@ -4,14 +4,12 @@ use auth_service::database_client::DatabaseClientTrait;
use auth_service::grpc::MyAuthService; use auth_service::grpc::MyAuthService;
use auth_service::session::session_service_client::SessionServiceClient; use auth_service::session::session_service_client::SessionServiceClient;
use dotenv::dotenv; use dotenv::dotenv;
use std::collections::HashMap;
use std::env; use std::env;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use tonic::transport::Server; use tonic::transport::Server;
use tracing::{debug, info, Level}; use tracing::info;
use utils::consul_registration; use tracing_subscriber::{fmt, EnvFilter};
use utils::multi_service_load_balancer::{LoadBalancingStrategy, MultiServiceLoadBalancer}; use utils::logging;
use utils::service_discovery::{get_kube_service_endpoints_by_dns}; use utils::service_discovery::{get_kube_service_endpoints_by_dns};
#[tokio::main] #[tokio::main]
@@ -19,13 +17,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load environment variables from .env // Load environment variables from .env
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); 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()); let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "50051".to_string());
@@ -45,7 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (mut health_reporter, health_service) = tonic_health::server::health_reporter(); let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<AuthServiceServer<MyAuthService>>().await; health_reporter.set_serving::<AuthServiceServer<MyAuthService>>().await;
println!("Authentication Service running on {}", addr); info!("Authentication Service running on {}", addr);
// Start the gRPC server // Start the gRPC server
tokio::spawn( tokio::spawn(

View File

@@ -9,7 +9,7 @@ dotenv = "0.15"
tokio = { version = "1.41.1", features = ["full"] } tokio = { version = "1.41.1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.3.18" tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] }
tonic = "0.12.3" tonic = "0.12.3"
prost = "0.13.4" prost = "0.13.4"
warp = "0.3.7" warp = "0.3.7"

View File

@@ -12,17 +12,15 @@ use std::env;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use tracing::Level; use tracing::Level;
use tracing_subscriber::EnvFilter;
use utils::logging;
use utils::service_discovery::{get_kube_service_endpoints_by_dns}; use utils::service_discovery::{get_kube_service_endpoints_by_dns};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());

View File

@@ -15,7 +15,7 @@ chrono = { version = "0.4.39", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
dotenv = "0.15" dotenv = "0.15"
tracing = "0.1" 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" prost = "0.13.3"
serde_json = "1.0.133" serde_json = "1.0.133"
async-trait = "0.1.83" async-trait = "0.1.83"

View File

@@ -11,17 +11,15 @@ use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tonic::transport::Server; use tonic::transport::Server;
use tracing::{info, Level}; use tracing::{info, Level};
use tracing_subscriber::EnvFilter;
use utils::logging;
use utils::redis_cache::RedisCache; use utils::redis_cache::RedisCache;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); 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()); let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "50052".to_string());

View File

@@ -13,7 +13,7 @@ tokio = { version = "1.41.1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
bytes = { version = "1.8.0", features = ["std", "serde"] } bytes = { version = "1.8.0", features = ["std", "serde"] }
tracing = "0.1" 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"] } bincode = { version = "2.0.0", features = ["derive", "serde"] }
thiserror = "2.0.3" thiserror = "2.0.3"
lazy_static = "1.5.0" lazy_static = "1.5.0"

View File

@@ -20,7 +20,8 @@ use tokio::sync::{Mutex, Semaphore};
use tokio::{select, signal}; use tokio::{select, signal};
use tracing::Level; use tracing::Level;
use tracing::{debug, error, info, warn}; 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 utils::service_discovery::{get_kube_service_endpoints_by_dns};
use warp::Filter; use warp::Filter;
@@ -58,12 +59,8 @@ const MAX_CONCURRENT_CONNECTIONS: usize = 100;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());

View File

@@ -9,7 +9,7 @@ dotenv = "0.15"
tokio = { version = "1.41.1", features = ["full"] } tokio = { version = "1.41.1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.3.18" tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] }
tonic = "0.12.3" tonic = "0.12.3"
prost = "0.13.4" prost = "0.13.4"
warp = "0.3.7" warp = "0.3.7"

View File

@@ -10,7 +10,8 @@ use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tonic::transport::Server; use tonic::transport::Server;
use tracing::Level; use tracing::Level;
use utils::consul_registration; use tracing_subscriber::{fmt, EnvFilter};
use utils::{consul_registration, logging};
use utils::redis_cache::RedisCache; use utils::redis_cache::RedisCache;
pub mod common { pub mod common {
@@ -24,12 +25,8 @@ pub mod api {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());

View File

@@ -19,3 +19,4 @@ hickory-resolver = "0.24.4"
rand = "0.8.5" rand = "0.8.5"
kube = { version = "0.99.0", features = ["derive"] } kube = { version = "0.99.0", features = ["derive"] }
k8s-openapi = { version = "0.24.0", features = ["v1_32"] } k8s-openapi = { version = "0.24.0", features = ["v1_32"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] }

View File

@@ -4,3 +4,4 @@ pub mod redis_cache;
pub mod service_discovery; pub mod service_discovery;
pub mod signal_handler; pub mod signal_handler;
pub mod multi_service_load_balancer; pub mod multi_service_load_balancer;
pub mod logging;

19
utils/src/logging.rs Normal file
View File

@@ -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");
}

View File

@@ -9,7 +9,7 @@ dotenv = "0.15"
tokio = { version = "1.41.1", features = ["full"] } tokio = { version = "1.41.1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.3.18" tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] }
tonic = "0.12.3" tonic = "0.12.3"
prost = "0.13.4" prost = "0.13.4"
warp = "0.3.7" warp = "0.3.7"

View File

@@ -1,20 +1,13 @@
use dotenv::dotenv; use dotenv::dotenv;
use std::collections::HashMap;
use std::env; use std::env;
use std::str::FromStr; use utils::{consul_registration, logging};
use tracing::{debug, Level};
use utils::consul_registration;
use utils::service_discovery::{get_kube_service_endpoints_by_dns, get_service_endpoints_by_dns}; use utils::service_discovery::{get_kube_service_endpoints_by_dns, get_service_endpoints_by_dns};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok(); dotenv().ok();
tracing_subscriber::fmt() let app_name = env!("CARGO_PKG_NAME");
.with_max_level( logging::setup_logging(app_name);
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string()))
.unwrap_or_else(|_| Level::INFO),
)
.init();
// Set the gRPC server address // Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string()); let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());