Exclude certain requests from logs based on request path

1. Caddy version (caddy version):

v2.4.6

2. How I run Caddy:

I run it the usual way using Caddyfile as the adapter.

caddy run --config /my/Caddyfile

a. System environment:

Ubuntu 18. systemd.

b. Command:

caddy run --config /my/Caddyfile

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.

d. My complete Caddyfile or JSON config:

This is the part I use to serve my blog:

alialhajji.com {
        root * /var/www/blog/public

        log {
                output file /var/log/caddy/blog-access.log
        }

        handle_errors {
                rewrite * /404.html
                file_server
        }

        encode gzip
        file_server
}

3. The problem I’m having:

In Caddy v1 I could exclude certain requests from the logs based on the request path:

log /path/to/log/file.log {
    except /statics /images /css /js /favicon.ico
}

Is there a way to do that in Caddy v2? I searched the docs and old discussions here but I could not find an answer.

4. Error messages and/or full log output:

5. What I already tried:

I used except inside the log directive but Caddy did not recognize it.

6. Links to relevant resources:

What are you trying to accomplish by doing this, exactly?

Hello Matt.
When someones visits my blog, all the requests sent from that page are logged (css files requests, images, etc).
This is too much noise in the logs, I want to exclude these requests from the logs.

As I said, this could be done in Caddy 1 as such:

log /path/to/log/file.log {
    except /statics /images /css /js /favicon.ico
}

all requests that match /statics /images /css /js /favicon.ico are not logged into /path/to/log/file.log

Ah right. A better way to reduce noise in the logs is to use sampling instead: JSON Config Structure - Caddy Documentation

Otherwise, simply ignore the log messages for paths you do not care about. Things are more efficient this way. See How Logging Works — Caddy Documentation

Could you confirm that there is no way to implement exempting the requests by the path from being logged as we were doing at Caddy1? I am trying to not log the api/health requests from the reverse proxy.

Not currently, no. There’s no way to configure Caddy to not emit access logs by request path.

I have a couple ideas of how we could make that possible, but it’ll take a bit of engineering effort to figure out a good way to do it (how the config surface will look, performance considerations, etc)

1 Like

I wrote up an issue for this here:

Feel free to subscribe to the issue to hear about progress. I’ll probably work on this soon (should be a pretty simple implementation).

2 Likes

I have an implementation here that can be tested:

1 Like

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