rustf 1.0.0

A simple MVC web framework inspired by Total.js
Documentation
//! Request logging middleware for RustF
//!
//! This middleware logs incoming requests with timing information and response status.
//! Optimized for performance with minimal allocations and conditional logging.
//!
//! # Performance Characteristics
//!
//! This optimized implementation has minimal overhead:
//! - **When logging is enabled**: ~1-5µs per request
//! - **When logging is disabled**: ~100ns per request (near-zero cost)
//!
//! Optimizations applied:
//! - No string clones (uses references from request)
//! - Conditional compilation based on log level
//! - Minimal context storage (single Instant)
//! - Lazy string formatting (only when logged)
//!
//! # Production Usage
//!
//! This middleware is suitable for production use, but consider:
//! - Use `RUST_LOG=warn` in production to disable request logs
//! - For high-traffic services (>10k req/s), consider sampling
//! - For detailed observability, consider structured logging (tracing crate)

use crate::context::Context;
use crate::error::Result;
use crate::middleware::{InboundAction, InboundMiddleware, OutboundMiddleware};
use async_trait::async_trait;
use std::time::Instant;

/// HTTP request logging middleware
///
/// Logs all incoming requests with method, path, response status, and timing.
///
/// # Performance
///
/// Optimized to minimize overhead:
/// - Zero allocations in hot path when logging is disabled
/// - Conditional logging (checks log level before formatting)
/// - Minimal context storage (only timing information)
///
/// # Example
///
/// ```rust,ignore
/// use rustf::middleware::builtin::LoggingMiddleware;
///
/// // In your app setup
/// app.middleware_from(|registry| {
///     registry.register_dual("logging", LoggingMiddleware::new());
/// });
/// ```
#[derive(Clone)]
pub struct LoggingMiddleware {
    pub name: String,
}

impl LoggingMiddleware {
    /// Create a new logging middleware
    pub fn new() -> Self {
        Self {
            name: "request_logger".to_string(),
        }
    }

    /// Create a logging middleware with a custom name
    pub fn with_name(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Default for LoggingMiddleware {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl InboundMiddleware for LoggingMiddleware {
    async fn process_request(&self, ctx: &mut Context) -> Result<InboundAction> {
        // Only perform logging work if info level is enabled
        if log::log_enabled!(log::Level::Info) {
            let start_time = Instant::now();

            // Use references - no clones needed
            let method = &ctx.req.method;
            let path = ctx.req.path();
            let ip = ctx.ip();

            // Conditional logging - only formats if enabled
            log::info!("→ {} {} from {}", method, path, ip);

            // Store only the start time - we'll read method/path from ctx.req in outbound phase
            let _ = ctx.set("logging_start_time", start_time);
        }

        // We want to process the response to log timing
        Ok(InboundAction::Capture)
    }

    fn name(&self) -> &'static str {
        "logging"
    }

    fn priority(&self) -> i32 {
        -1000 // Very high priority (runs first)
    }
}

#[async_trait]
impl OutboundMiddleware for LoggingMiddleware {
    async fn process_response(&self, ctx: &mut Context) -> Result<()> {
        // Only perform logging work if info level is enabled
        if log::log_enabled!(log::Level::Info) {
            if let Some(start_time) = ctx.get::<Instant>("logging_start_time") {
                let elapsed = start_time.elapsed();

                // Read directly from request - no clones stored
                let method = &ctx.req.method;
                let path = ctx.req.path();
                let status = ctx.res.as_ref().map(|r| r.status.as_u16()).unwrap_or(500);

                // Conditional logging - only formats if enabled
                log::info!("← {} {} {} in {:?}", method, path, status, elapsed);
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http::Request;
    use crate::views::ViewEngine;
    use std::sync::Arc;

    #[tokio::test]
    async fn test_logging_middleware() {
        let middleware = LoggingMiddleware::new();

        // Create test context
        let mut request = Request::default();
        request.method = "GET".to_string();
        request.uri = "/test".to_string();

        let views = Arc::new(ViewEngine::from_directory("views"));

        let mut ctx = Context::new(request, views);

        // Test inbound processing
        let action = middleware.process_request(&mut ctx).await.unwrap();
        assert!(matches!(action, InboundAction::Capture));

        // Verify context has timing data (only if logging is enabled)
        if log::log_enabled!(log::Level::Info) {
            assert!(ctx.get::<Instant>("logging_start_time").is_some());
        }

        // Test outbound processing (Context already has response)
        middleware.process_response(&mut ctx).await.unwrap();
    }

    #[tokio::test]
    async fn test_logging_middleware_minimal_overhead() {
        // This test verifies that when logging is disabled, there's minimal overhead
        let middleware = LoggingMiddleware::new();

        let mut request = Request::default();
        request.method = "POST".to_string();
        request.uri = "/api/data".to_string();

        let views = Arc::new(ViewEngine::from_directory("views"));
        let mut ctx = Context::new(request, views);

        // Process request - should be fast even with logging disabled
        let start = Instant::now();
        let _ = middleware.process_request(&mut ctx).await.unwrap();
        let inbound_duration = start.elapsed();

        // Process response
        let start = Instant::now();
        let _ = middleware.process_response(&mut ctx).await.unwrap();
        let outbound_duration = start.elapsed();

        // When logging is disabled, total overhead should be < 10µs
        // When enabled, should be < 50µs (depends on log backend)
        let total = inbound_duration + outbound_duration;

        // This is a sanity check - actual performance will vary by system
        assert!(
            total.as_micros() < 100,
            "Logging middleware overhead too high: {:?}",
            total
        );
    }
}