Skip to content

Custom Access Log For Response #281

Description

@EverydayDiesel

Hello,

This example shows how to get a custom access log at the time of request. How can I get the information at the response (I need the return HTTP response code ie 200, 404, 418)

    #include <httpserver.hpp>
    #include <iostream>

    using namespace httpserver;

    void custom_access_log(const std::string& url) {
        std::cout << "ACCESSING: " << url << std::endl;
    }

    class hello_world_resource : public http_resource {
    public:
        const std::shared_ptr<http_response> render(const http_request&) {
            return std::shared_ptr<http_response>(new string_response("Hello, World!"));
        }
    };

    int main(int argc, char** argv) {
        webserver ws = create_webserver(8080)
            .log_access(custom_access_log);

        hello_world_resource hwr;
        ws.register_resource("/hello", &hwr);
        ws.start(true);

        return 0;
    }

Activity

  1. Lotharyx commented on Mar 14, 2023

    @Lotharyx

    More to the point: It would be a nice enhancement to have the log_access and log_error handlers take the request object as the argument (rather than std::string) so that the handler can format the log message as desired.

  2. etr commented on Jul 23, 2026

    @etr
    Owner

    Resolved in libhttpserver 2.0.0. You can now observe the response — including the HTTP status code (200/404/418…), bytes sent, and timing — via the response_sent lifecycle hook, rather than only the URL at request time. See examples/clf_access_log.cpp and the README's Lifecycle Hooks section.

    Closing as resolved by 2.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions