Don't add log statements for every site served by caddy

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

I have many sites in my Caddyfile, to make two examples:

(handle_errors) {
    handle_errors {
        respond "{http.error.status_code} {http.error.status_text}"
    }
}

nognu.de {
import handle_errors
root * /var/www/nognu.de
}

etc…

What I would like to achieve to have the same logic for handle_errors like for handle_logs but this does not work:

 (handle_logging) {
    log {
        format console
        format output file /var/log/caddy/{uri}
    }
}

Or whatever I put on uri. I don’t want to duplicate the log a hundred times. Any hints?

You can add arguments to snippets (see https://caddyserver.com/docs/caddyfile/concepts#snippets, e.g.:

(handle_logging) {
    log {
        format console
        format output file /var/log/caddy/{args.0}
        }
}

and using it like

nognu.de {
import handle_errors
import handle_logging nognu.de
root * /var/www/nognu.de
}

will result in a log file /var/log/caddy/nognu.de .

3 Likes