feat: Add colorized request logging
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user