How can i log the request and response body of each request

1. The problem I’m having:

I’m trying to implement logging for my caddy config with Grafana and so far i have been successful. But the logs do not contain request body or response body.

2. Error messages and/or full log output:

there is no error, I’m trying to implement something new

3. Caddy version: 2.8.4

4. How I installed and ran Caddy:

I use the latest docker image version

a. System environment:

Docker on Ubuntu

b. My complete Caddy config:

{
    admin :2019
    log {
        output stdout
        format json
    }

    servers {
        metrics
    }
}

example.com {
        tls {
        dns cloudflare {env.CLOUDFLARE_API_TOKEN}
    }

    log {
        output stdout
        format json
    }

    reverse_proxy /end* app-two:3000
    reverse_proxy app-one:3000
}

The trouble with that is the request/response bodies are typically data streams, so you can’t log it without also consuming the stream, which means you need to buffer the body in memory to replay it when the request handling also needs access to it. This is inefficient, and it throws away a lot of performance by doing that.

There is a {http.request.body} placeholder (see JSON Config Structure - Caddy Documentation) to buffer the request body and emit it somewhere, but it cannot be added to access logs.

I think you’ll need to reconsider what you’re trying to do.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.