Is there a placeholder within log that has the domain host?

I’d like to use the domain host in the name of the log file. I’m trying to do this with placeholders, but perhaps the log doesn’t have this placeholder.

My Caddy version (caddy version):

$ caddy version
v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

My Caddyfile looks like:

mydomain.com {
        reverse_proxy * someaddress:someport
        log {
                output file mydomain.com.log
        }
}

I tried creating the following that should be an equivalent Caddyfile:

mydomain.com {
        reverse_proxy * someaddress:someport
        log {
                output file {http.request.host}.log
        }
}

When I try the above I get:

2020/05/05 16:37:29.503 INFO    using adjacent Caddyfile
2020/05/05 16:37:29.527 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["127.0.0.1:2019", "localhost:2019", "[::1]:2019"]}
run: loading initial config: loading new config: setting up custom log 'log1': loading log writer module: loading module 'file': provision caddy.logging.writers.file: invalid filename for log file: unrecognized placeholder {http.request.host}
start: caddy process exited with error: exit status 1

I want to do this because I have over 40 domain names that I need to change from caddy.log and it would be great if I can just replace all of them simultaneously.

Log files are opened before any HTTP requests come in, i.e. out of the scope/context of an HTTP request. Imagine if we were opening and closing log files at every request – it’d be super slow.

So, log files are opened only once (at config load), and closed only once (at server shutdown).

Also, writing arbitrary files on your disk based on random input from Internet clients is a bad idea. :slight_smile:

With structured logging, you don’t need to write different hosts to different files. (That’s the point of structure.) See this article for more information: How Logging Works — Caddy Documentation

1 Like

Okay, thanks!

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