Function aitch::middlewares::with_stdout_logging[][src]

pub fn with_stdout_logging<B: Body>(
    handler: impl Handler<B>
) -> impl Handler<B>

Middleware which outputs details of HTTP requests/responses to stdout.

This middleware wraps another HTTP handler, and logs details about each HTTP request (method and URI) and the associated response (status) to stdout. The logging only occurs when the Responder returned from the Handler resolved to a http::Response.

This middleware is intended to help during development. It is expected that any production application creates its own version of this middleware, to integrate it into the application's logging infrastructure.

Responder: ../trait.Responder.html Handler: ../trait.Handler.html http::Response: https://docs.rs/http/0.1.7/http/response/struct.Response.html

Example

extern crate aitch;
extern crate http;

use aitch::servers::hyper::Server;
use aitch::{middlewares, Responder, ResponseBuilder, Result};
use http::Request;

fn handler(_req: Request<()>, mut resp: ResponseBuilder) -> impl Responder {
    resp.body("Hello, world!".to_owned())
}

fn main() -> Result<()> {
    let wrapped = middlewares::with_stdout_logging(handler);

    let addr = "127.0.0.1:3000".parse()?;
    println!("Listening on http://{}", addr);
    Server::new(addr, wrapped)?.run()
}

Which outputs the following in response to requests:

This example is not tested
Listening on http://127.0.0.1:3000"
GET / 200
GET /path/doesnt/matter/in/this/example 200
...