shared-framework 0.0.17

Reusable building blocks for HTTP services — Hyper routing, SeaORM data layer, validation, OpenAPI docs, jobs, queues, cache.
Documentation
//! Monitoring helpers for logging request outcomes.
//!
//! [`RequestLogger`] records one `monitor`-target log line per completed request with path,
//! method, status, and latency. Call [`RequestLogger::start`] before handling and
//! [`RequestLogger::log`] after to emit the measurement.
//! ```ignore
//! let start = RequestLogger::start();
//! // ... handle request, obtain `status` ...
//! RequestLogger::log("/users/list", "GET", status, start.elapsed());
//! ```

use std::time::Instant;

/// Emits one `monitor`-target log line per completed request with path, method, status, and latency.
pub struct RequestLogger;

impl RequestLogger {
    /// Logs a completed request with its path, method, status code, and latency.
    pub fn log(path: &str, method: &str, status: u16, latency: std::time::Duration) {
        tracing::trace!(
            target: "monitor",
            path = %path,
            method = %method,
            status = status,
            latency_ms = latency.as_millis() as u64,
            "Request completed"
        );
    }

    /// Captures the start instant for a latency measurement; call `elapsed()` on it when logging.
    pub fn start() -> Instant { Instant::now() }
}