How do I log both the request length and response length?

1. Caddy version (caddy version): v2.4.5

2. How I run Caddy:

I run caddy using the JSON config. I write a Caddyfile then Adapt it and load the config.
It’s not containerised.

a. System environment:

Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-88-generic x86_64)

b. Command:

caddy start

c. Service/unit/compose file:

Not being used.

d. My complete Caddyfile or JSON config:

{
        on_demand_tls {
                ask http://my_server/allowed
                interval 2m
                burst 5
        }
        order rate_limit before basicauth
        log {
                level DEBUG
                output file /var/log/caddy/access.log {
                        roll_size 50mb
                        roll_keep 50
                }
        }
        storage redis {
                host "redis_server"
                port 6358
                password "password"
                db 1
                key_prefix "caddy-certs"
                value_prefix ""
                timeout 5
                tls_enabled "false"
                tls_insecure "true"
        }
}

3. The problem I’m having:

I want to log the content-length for the requests and responses that passes through Caddy, I would also like to log the time taken if possible.
I’ve been looking around the forum and documentation but haven’t seen a way to log any of these yet, I would really appreciate some assistance here, thanks!

4. Error messages and/or full log output:

N/A

5. What I already tried:

I’ve looked through the docs and searched the forum and Googled for a solution, haven’t seen one yet though

6. Links to relevant resources:

Access logs are enabled by adding log to your individual site blocks instead of in global options. You’ve configured Caddy to write all its logs to your access.log file, rather than just access logs.

1 Like

Thanks for the response, but I am asking how to get the content-lengths for the request and response, not how to write an access log, do you know how to achieve this?

You enable an access log.

~/Projects
➜ caddy version
v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=
~/Projects
➜ cat Caddyfile
http://localhost:8080
log {
  output file ./access.log
}
file_server
~/Projects
➜ echo "Stuff" > index.html
~/Projects
➜ curl -v localhost:8080/index.html
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /index.html HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Length: 6
< Content-Type: text/html; charset=utf-8
< Etag: "r0u5ne6"
< Last-Modified: Mon, 11 Oct 2021 23:09:14 GMT
< Server: Caddy
< Date: Mon, 11 Oct 2021 23:09:16 GMT
<
Stuff
* Connection #0 to host localhost left intact
* Closing connection 0
~/Projects
➜ cat access.log
2021/10/11 23:09:16.048	info	http.log.access.log0	handled request	{"request": {"remote_addr": "[::1]:63231", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8080", "uri": "/index.html", "headers": {"User-Agent": ["curl/7.64.1"], "Accept": ["*/*"]}}, "common_log": "::1 - - [12/Oct/2021:09:09:16 +1000] \"GET /index.html HTTP/1.1\" 200 6", "user_id": "", "duration": 0.0109075, "size": 6, "status": 200, "resp_headers": {"Server": ["Caddy"], "Etag": ["\"r0u5ne6\""], "Content-Type": ["text/html; charset=utf-8"], "Last-Modified": ["Mon, 11 Oct 2021 23:09:14 GMT"], "Accept-Ranges": ["bytes"], "Content-Length": ["6"]}}
~/Projects
➜ echo '{"request": {"remote_addr": "[::1]:63231", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8080", "uri": "/index.html", "headers": {"User-Agent": ["curl/7.64.1"], "Accept": ["*/*"]}}, "common_log": "::1 - - [12/Oct/2021:09:09:16 +1000] \\"GET /index.html HTTP/1.1\\" 200 6", "user_id": "", "duration": 0.0109075, "size": 6, "status": 200, "resp_headers": {"Server": ["Caddy"], "Etag": ["\\"r0u5ne6\\""], "Content-Type": ["text/html; charset=utf-8"], "Last-Modified": ["Mon, 11 Oct 2021 23:09:14 GMT"], "Accept-Ranges": ["bytes"], "Content-Length": ["6"]}}' | jq
{
  "request": {
    "remote_addr": "[::1]:63231",
    "proto": "HTTP/1.1",
    "method": "GET",
    "host": "localhost:8080",
    "uri": "/index.html",
    "headers": {
      "User-Agent": [
        "curl/7.64.1"
      ],
      "Accept": [
        "*/*"
      ]
    }
  },
  "common_log": "::1 - - [12/Oct/2021:09:09:16 +1000] \"GET /index.html HTTP/1.1\" 200 6",
  "user_id": "",
  "duration": 0.0109075,
  "size": 6,
  "status": 200,
  "resp_headers": {
    "Server": [
      "Caddy"
    ],
    "Etag": [
      "\"r0u5ne6\""
    ],
    "Content-Type": [
      "text/html; charset=utf-8"
    ],
    "Last-Modified": [
      "Mon, 11 Oct 2021 23:09:14 GMT"
    ],
    "Accept-Ranges": [
      "bytes"
    ],
    "Content-Length": [
      "6"
    ]
  }
}

The headers containing the information you seek are logged.

3 Likes

Thanks! I’ll try it out :smiley: