feat: Add colorized request logging
This commit is contained in:
17
Cargo.lock
generated
17
Cargo.lock
generated
@@ -200,6 +200,16 @@ dependencies = [
|
|||||||
"cc",
|
"cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colored"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"windows-sys 0.52.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "core-foundation-sys"
|
name = "core-foundation-sys"
|
||||||
version = "0.8.7"
|
version = "0.8.7"
|
||||||
@@ -213,6 +223,7 @@ dependencies = [
|
|||||||
"axum",
|
"axum",
|
||||||
"axum-server",
|
"axum-server",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"colored",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -508,6 +519,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.186"
|
version = "0.2.186"
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ serde_json = "1.0"
|
|||||||
axum-server = { version = "0.7", features = ["tls-rustls"] }
|
axum-server = { version = "0.7", features = ["tls-rustls"] }
|
||||||
tower-http = { version = "0.5", features = ["trace"] }
|
tower-http = { version = "0.5", features = ["trace"] }
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
colored = "2.1"
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ DHL is a lightweight, configurable HTTP/HTTPS mock server built with Rust and Ax
|
|||||||
- **Live Configuration Reloading**: Changes to `config.json` (routes, status codes, etc.) and response files are applied immediately without restarting the server.
|
- **Live Configuration Reloading**: Changes to `config.json` (routes, status codes, etc.) and response files are applied immediately without restarting the server.
|
||||||
- **Multi-Route Support**: Define as many paths as you need.
|
- **Multi-Route Support**: Define as many paths as you need.
|
||||||
- **JSON Response Files**: Responses are loaded from external JSON files.
|
- **JSON Response Files**: Responses are loaded from external JSON files.
|
||||||
- **Request Logging**: Detailed logging of incoming requests, including methods, paths, and headers.
|
- **Colorized Request Logging**: Detailed logging of incoming requests with terminal colors. Status codes are color-coded for quick identification:
|
||||||
|
- <span style="color:green">**2xx Success**</span>: Green
|
||||||
|
- <span style="color:yellow">**4xx Client Error**</span>: Yellow
|
||||||
|
- <span style="color:red">**5xx Server Error**</span>: Red
|
||||||
- **Header Masking**: Sensitive headers like `Authorization` can be masked in logs.
|
- **Header Masking**: Sensitive headers like `Authorization` can be masked in logs.
|
||||||
- **HTTPS/TLS Support**: Optional TLS support for secure connections.
|
- **HTTPS/TLS Support**: Optional TLS support for secure connections.
|
||||||
- **Automatic Default Generation**: Creates default configuration and response files if they are missing.
|
- **Automatic Default Generation**: Creates default configuration and response files if they are missing.
|
||||||
|
|||||||
25
src/main.rs
25
src/main.rs
@@ -17,6 +17,7 @@ use crate::config::Config;
|
|||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
use crate::handlers::{handler, fallback_handler};
|
use crate::handlers::{handler, fallback_handler};
|
||||||
use crate::middleware::logging_middleware;
|
use crate::middleware::logging_middleware;
|
||||||
|
use colored::*;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
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) {
|
fn print_banner(config: &Config) {
|
||||||
println!("------------------------------------------");
|
println!("{}", "------------------------------------------".bright_black());
|
||||||
println!(" ____ ___ _ ______________ ");
|
println!("{}", " ____ ___ _ ______________ ".blue().bold());
|
||||||
println!(" / __ |/ | / | / / _/ ____/ / ");
|
println!("{}", " / __ |/ | / | / / _/ ____/ / ".blue().bold());
|
||||||
println!(" / / / / /| | / |/ // // __/ / / ");
|
println!("{}", " / / / / /| | / |/ // // __/ / / ".blue().bold());
|
||||||
println!(" / /_/ / ___ |/ /| // // /___/ /___ ");
|
println!("{}", " / /_/ / ___ |/ /| // // /___/ /___ ".blue().bold());
|
||||||
println!(" /_____/_/ |_/_/ |_/___/_____/_____/ ");
|
println!("{}", " /_____/_/ |_/_/ |_/___/_____/_____/ ".blue().bold());
|
||||||
println!("------------------------------------------");
|
|
||||||
println!(" ");
|
println!(" ");
|
||||||
println!("------ HTTP LISTENER ------");
|
println!("{}", "Config: ".bold().underline());
|
||||||
println!("host : {}", config.host);
|
println!(" ");
|
||||||
println!("port : {}", config.port);
|
println!("{:<14}: {}", "host".bright_black(), config.host.yellow());
|
||||||
println!("Live Config Reloading: ENABLED");
|
println!("{:<14}: {}", "port".bright_black(), config.port.to_string().yellow());
|
||||||
println!("------------------------------------------");
|
println!("{:<14}: {}", "Live Reload".bright_black(), "ENABLED".green().bold());
|
||||||
|
println!("{}", "------------------------------------------".bright_black());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use axum::{
|
|||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::state::SharedState;
|
use crate::state::SharedState;
|
||||||
|
use colored::*;
|
||||||
|
|
||||||
// Note: Axum body might need to be imported differently depending on version
|
// 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.
|
// 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
|
// Load config live for masking settings
|
||||||
let config = crate::config::Config::load();
|
let config = crate::config::Config::load();
|
||||||
|
|
||||||
println!("---- REQUEST START {} ----", start_time.format("%Y-%m-%d %H:%M:%S"));
|
let method = req.method().clone();
|
||||||
println!("Method: {}", req.method());
|
let path = req.uri().path().to_string();
|
||||||
println!("Path: {}", req.uri().path());
|
|
||||||
println!("Headers:");
|
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() {
|
for (name, value) in req.headers() {
|
||||||
let name_str = name.as_str();
|
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));
|
config.masking.headers.iter().any(|h| h.eq_ignore_ascii_case(name_str));
|
||||||
|
|
||||||
if should_mask {
|
if should_mask {
|
||||||
println!(" {}: ***", name_str);
|
println!(" {}: {}", name_str.bright_black(), "***".yellow());
|
||||||
} else {
|
} 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 end_time = Local::now();
|
||||||
let duration = start_instant.elapsed();
|
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
|
response
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user