feat: Add colorized request logging

This commit is contained in:
danielvici123
2026-06-09 20:13:39 +02:00
parent 36a3b96c12
commit 520fd4f1e6
5 changed files with 59 additions and 20 deletions

View File

@@ -17,6 +17,7 @@ use crate::config::Config;
use crate::state::AppState;
use crate::handlers::{handler, fallback_handler};
use crate::middleware::logging_middleware;
use colored::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -71,17 +72,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
fn print_banner(config: &Config) {
println!("------------------------------------------");
println!(" ____ ___ _ ______________ ");
println!(" / __ |/ | / | / / _/ ____/ / ");
println!(" / / / / /| | / |/ // // __/ / / ");
println!(" / /_/ / ___ |/ /| // // /___/ /___ ");
println!(" /_____/_/ |_/_/ |_/___/_____/_____/ ");
println!("------------------------------------------");
println!("{}", "------------------------------------------".bright_black());
println!("{}", " ____ ___ _ ______________ ".blue().bold());
println!("{}", " / __ |/ | / | / / _/ ____/ / ".blue().bold());
println!("{}", " / / / / /| | / |/ // // __/ / / ".blue().bold());
println!("{}", " / /_/ / ___ |/ /| // // /___/ /___ ".blue().bold());
println!("{}", " /_____/_/ |_/_/ |_/___/_____/_____/ ".blue().bold());
println!(" ");
println!("------ HTTP LISTENER ------");
println!("host : {}", config.host);
println!("port : {}", config.port);
println!("Live Config Reloading: ENABLED");
println!("------------------------------------------");
println!("{}", "Config: ".bold().underline());
println!(" ");
println!("{:<14}: {}", "host".bright_black(), config.host.yellow());
println!("{:<14}: {}", "port".bright_black(), config.port.to_string().yellow());
println!("{:<14}: {}", "Live Reload".bright_black(), "ENABLED".green().bold());
println!("{}", "------------------------------------------".bright_black());
}

View File

@@ -7,6 +7,7 @@ use axum::{
use chrono::Local;
use std::time::Instant;
use crate::state::SharedState;
use colored::*;
// Note: Axum body might need to be imported differently depending on version
// but since the original used axum::body::Body, I'll stick to that or similar.
@@ -23,10 +24,13 @@ pub async fn logging_middleware(
// Load config live for masking settings
let config = crate::config::Config::load();
println!("---- REQUEST START {} ----", start_time.format("%Y-%m-%d %H:%M:%S"));
println!("Method: {}", req.method());
println!("Path: {}", req.uri().path());
println!("Headers:");
let method = req.method().clone();
let path = req.uri().path().to_string();
println!("{}", format!("---- REQUEST START {} ----", start_time.format("%Y-%m-%d %H:%M:%S")).blue().bold());
println!("{}: {}", "Method".bright_black(), method);
println!("{}: {}", "Path".bright_black(), path);
println!("{}:", "Headers".bright_black());
for (name, value) in req.headers() {
let name_str = name.as_str();
@@ -35,9 +39,9 @@ pub async fn logging_middleware(
config.masking.headers.iter().any(|h| h.eq_ignore_ascii_case(name_str));
if should_mask {
println!(" {}: ***", name_str);
println!(" {}: {}", name_str.bright_black(), "***".yellow());
} else {
println!(" {}: {:?}", name_str, value);
println!(" {}: {:?}", name_str.bright_black(), value);
}
}
@@ -45,8 +49,21 @@ pub async fn logging_middleware(
let end_time = Local::now();
let duration = start_instant.elapsed();
let status = response.status();
println!("--- REQUEST END {} - {:?} ---------", end_time.format("%H:%M:%S"), duration);
let status_colored = if status.is_success() {
status.to_string().green()
} else if status.is_server_error() {
status.to_string().red()
} else if status.is_client_error() {
status.to_string().yellow()
} else {
status.to_string().cyan()
};
println!("{}: {}", "Status".bright_black(), status_colored.bold());
println!("{}", format!("--- REQUEST END {} - {:?} ---------", end_time.format("%H:%M:%S"), duration).blue().bold());
println!();
response
}