Skip to main content

rustbasic_core/middleware/
logging.rs

1use crate::requests::Request;
2use crate::middleware::Next;
3use crate::router::Response;
4use crate::colored::Colorize;
5
6pub async fn logging_middleware(
7    req: Request,
8    next: Next,
9) -> Response {
10    let method = req.method.clone();
11    let path = req.path.clone();
12    let ip = req.ip_address.clone();
13
14    // Log ke Terminal (Format: [HTTP] TIMESTAMP METHOD PATH from IP)
15    let method_str = method.as_str();
16    let method_colored = match method_str {
17        "GET" => method_str.green(),
18        "POST" => method_str.blue(),
19        "PUT" => method_str.yellow(),
20        "DELETE" => method_str.red(),
21        _ => method_str.white(),
22    };
23
24    println!(
25        "[{}] {} {:<6} {} from {}",
26        "HTTP".magenta().bold(),
27        chrono::Local::now().format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string().dimmed(),
28        method_colored.bold(),
29        path.cyan(),
30        ip.yellow()
31    );
32
33    // Log ke File (Tanpa warna via custom logger)
34    crate::logger::log(crate::logger::Level::Info, &format!("Request method={} path={} ip={}", method, path, ip));
35
36    next.run(req).await
37}