Seperate log file per location

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

Installed through the repository on Ubuntu 20.04. Configuration in /etc/caddy/Caddyfile

a. System environment:

Ubuntu 20.04, systemd

b. Command:

systemctl start caddy

c. Service/unit/compose file:


ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile


d. My complete Caddyfile or JSON config:

(logging) {
  log {
    output file /var/log/caddy/{args.0}.log

:80/test/* {
  redir *
  import logging test

:80/test2/* {
  redir *
  import logging test2

3. The problem I’m having:

I’m trying to create a config where requests to different paths end up in different log files. The Caddyfile above is the closest I got to having this, but I’m having trouble getting it to work.

4. Error messages and/or full log output:

There’s not really error messages, but the second log file for test2 is never created. If I use different port numbers (:80 and :81 for example) both log files are created.

5. What I already tried:

A million things. Handlers, routes, adding custom fields to the logs. My end goal is a way to add some sort of tag to a log entry based on what path the client visited. So for example:

/test -> add tag “Path-01”
/test2 -> add tag “Path-02”

The logger uses hostname matching, not path matching currently, unfortunately.

@matt may be able to comment on whether it’s possible to implement later

Thanks for the clarification. Is there any other hack / workaround you can think of?

I don’t need seperate logfiles specifically, my use-case simply demands a way to see a difference in the logs based on what path was visited (without being visible to the user). I almost got what I wanted using maps:

localhost {
  map http.request.uri.path backend {
    default test 
    /test2 test-2
  import logging {backend}         

However, I get an error message saying:

Jul 31 15:13:23 ip-10-0-0-220 caddy[17966]: run: loading initial config: loading new config: setting up custom log 'log0': loading log writer module: loading module 'file': provision caddy.logging.writers.file: invalid filename for log file: unrecognized placeholder {backend}

I’m guessing this is because the {backend} variable is dynamic, and the logger is initialized before that.

Is there maybe a way to add a custom variable to the log output?

Well, the paths are in the logs, so you use tools like grep and/or jq to parse your logs in the one file. Caddy uses structured logging so you can do all kinds of stuff with that.