Function log_request

Source
pub async fn log_request(
    __arg0: ConnectInfo<SocketAddr>,
    req: Request<Body>,
    next: Next,
) -> Result<Response, StatusCode>
Expand description

Logs method, path, IP address, user ID (if authenticated), origin, and user-agent for each incoming HTTP request. Automatically skips CORS preflight OPTIONS requests.

This middleware can help trace incoming traffic, debug authentication issues, and monitor frontend usage patterns.

§Usage:

Apply this middleware globally using:

use axum::Router;
use axum::middleware::from_fn;
use api::auth::middleware::log_request;

let app = Router::new().layer(from_fn(log_request));

§Fields Logged:

  • method: HTTP method used (GET, POST, etc.)
  • path: Requested URI path
  • ip: Remote IP address of the client
  • user: User ID if authenticated, 0 if not
  • origin: Value of the Origin header if present
  • user_agent: Value of the User-Agent header if present