Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access response status code in filter #140

Closed
jacobh opened this issue Dec 5, 2018 · 2 comments
Closed

Access response status code in filter #140

jacobh opened this issue Dec 5, 2018 · 2 comments

Comments

@jacobh
Copy link

jacobh commented Dec 5, 2018

I'm trying to write my own logging filters, and I've got most of the way, but I'm unsure if it's possible to access the response status code and logged it. Here's a truncated example of my code at the moment

fn main() {
    // logger
    let logger = new_slog_logger();
    let logging_pre_filter = warp::header::<String>("x-request-id")
        .or(warp::any().map(|| uuid::Uuid::new_v4().hyphenated().to_string()))
        .unify()
        .and(warp::method())
        .and(warp::path::full())
        .map(
            move |request_id: String,
                  method: http::method::Method,
                  path: warp::filters::path::FullPath| {
                let request_logger = logger.new(o!(
                "request_id" => request_id,
                "method" => method.to_string(),
                "path" => path.as_str().to_string(),
                ));
                debug!(request_logger, "Request received");
                warp::ext::set(request_logger)
            },
        )
        .untuple_one();

    let logging_post_filter = warp::ext::get::<slog::Logger>()
        .map(|request_logger: slog::Logger| {
            // somehow get response status code here
            debug!(
                request_logger,
                "Request completed";
            );
        })
        .untuple_one();

    let app = logging_pre_filter
        .and(...routes...)
        .and(logging_post_filter);

    warp::serve(app).run(([127, 0, 0, 1], 8000))
}

any guidance would be appreciated!

@seanmonstar
Copy link
Owner

Hm, the problem is that you can return any impl Reply, which may not have a status code available (or a Rejection). One solution to that more generic problem might be adding public methods to Reply, like status. I hadn't decided if that would be a good thing, as it can make eventually implementing Reply externally more annoying...


As for custom logging, we could see about fleshing out the warp::log filters. There's the simplest warp::log("logger_name"), but there are additional pieces in that module meant to allow plugging in custom loggers.

@jxs
Copy link
Collaborator

jxs commented Jul 7, 2020

this is now achievable with the Filter to Service conversion.
Also see #16 (comment)

@jxs jxs closed this as completed Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants