pub struct LoggingMiddleware { /* private fields */ }Expand description
Django-style request logging middleware with colored output
Outputs request logs in Django’s runserver format with latency:
[DD/Mon/YYYY HH:MM:SS] "METHOD /path HTTP/1.1" STATUS SIZE LATENCYms
Status codes are color-coded:
- 2xx: Green (success)
- 3xx: Cyan (redirect)
- 4xx: Yellow (client error)
- 5xx: Red (server error)
§Examples
use std::sync::Arc;
use reinhardt_middleware::LoggingMiddleware;
use reinhardt_http::{Handler, Middleware, Request, Response};
use hyper::{Method, Version, HeaderMap, StatusCode};
use bytes::Bytes;
struct TestHandler;
#[async_trait::async_trait]
impl Handler for TestHandler {
async fn handle(&self, _request: Request) -> reinhardt_core::exception::Result<Response> {
Ok(Response::new(StatusCode::OK).with_body(Bytes::from("OK")))
}
}
let middleware = LoggingMiddleware::new();
let handler = Arc::new(TestHandler);
let request = Request::builder()
.method(Method::GET)
.uri("/api/users")
.version(Version::HTTP_11)
.headers(HeaderMap::new())
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::OK);
// Logs: [15/Dec/2024 10:30:45] "GET /api/users HTTP/1.1" 200 2 0msImplementations§
Source§impl LoggingMiddleware
impl LoggingMiddleware
Sourcepub fn with_config(config: LoggingConfig) -> Self
pub fn with_config(config: LoggingConfig) -> Self
Create a logging middleware with custom configuration
Sourcepub fn production() -> Self
pub fn production() -> Self
Create a production-ready logging middleware
Uses LoggingConfig::production() which disables raw value logging.
Trait Implementations§
Source§impl Default for LoggingMiddleware
impl Default for LoggingMiddleware
Source§impl Middleware for LoggingMiddleware
impl Middleware for LoggingMiddleware
Auto Trait Implementations§
impl Freeze for LoggingMiddleware
impl RefUnwindSafe for LoggingMiddleware
impl Send for LoggingMiddleware
impl Sync for LoggingMiddleware
impl Unpin for LoggingMiddleware
impl UnsafeUnpin for LoggingMiddleware
impl UnwindSafe for LoggingMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().