Multiple Log Files

1. Caddy version (caddy version):

2.3.0

2. How I run Caddy:

run as system daemon at startup by the Ubuntu Server systemctl enable function using a simple Caddyfile entry

a. System environment:

Ubuntu 20.04 Server on AWS, (5.4.0) using PHP 7.4-fpm

b. Command:

systemctl [start/restart] caddy

c. Service/unit/compose file:

????

d. My complete Caddyfile or JSON config:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.

train.zambia.discon3.org {
        root * /var/www/html/zambiatrain/webpages
        php_fastcgi unix//run/php/php7.4-fpm.sock
        file_server
        log {
                output file /var/log/caddy/train-info {
                        roll_size 5MiB
                        roll_keep_for 720h
                }
                format json
                level INFO
        }
}

stage.zambia.discon3.org {
        root * /var/www/html/zambiastage/webpages
        php_fastcgi unix//run/php/php7.4-fpm.sock
        file_server
        log {
                output file /var/log/caddy/stage-info {
                        roll_size 5MiB
                        roll_keep_for 720h
                }
                format json
                level INFO
        }
}

zambia.discon3.org {
        root * /var/www/html/zambiaprod/webpages
        php_fastcgi unix//run/php/php7.4-fpm.sock
        file_server
        log {
                output file /var/log/caddy/prod-info {
                        roll_size 5MiB
                        roll_keep_for 720h
                }
                format json
                level INFO
        }
}

3. The problem I’m having:

I’m trying to build multiple log files per ‘server’ entry. With different criteria. I tried putting two log clauses in the config, it added the logs to the json but only assigned the second of the two log clauses to the server.

(the documentation says “lots of log files are good”, but how?)

4. Error messages and/or full log output:

It only outputs the second log entry (ok, the file above has only 1 entry, as I had to back it down, the earlier log entry was the same, except it had level: error and a file name with error in it.

5. What I already tried:

I had tried:

train.zambia.discon3.org {
root * /var/www/html/zambiatrain/webpages
php_fastcgi unix//run/php/php7.4-fpm.sock
file_server
log {
output file /var/log/caddy/train-info {
roll_size 5MiB
roll_keep_for 720h
}
format json
level INFO
}
log {
output file /var/log/caddy/train-error {
roll_size 5MiB
roll_keep_for 720h
}
format json
level ERROR
}
}

but this just chose only the ERROR variant to log, but the JSON showed it did parse and create both log clauses.

6. Links to relevant resources:

??

Unfortunately it’s not possible to have multiple access log files per site configured from the Caddyfile. It’s a limitation of how Caddyfile config is mapped to JSON.

If you need more fine-grained control of the logs, then you can do it by configuring it directly in the JSON config. Use caddy adapt to get your JSON config from your Caddyfile.

1 Like

I have the following code in my Caddyfile… Which works very well…

(logging) {
        log {
                output file /var/log/caddy-{args.0}-access.log {
                        roll true
                        roll_size 1Mib
                        roll_local_time true
                        roll_keep 24
                        roll_keep_for 7d
                }
        }
}

ssl.test.com {
        import logging ssl
        respond "Check SSL"
}

proxy.test.com {
        import logging proxy
        respond "Caddy Proxy Up!"
}

@MagnuM2K that’s not the question that was asked – they wanted to know how to have multiple log files for one site. And that’s not possible. You can only have one logger configured per site, when using the Caddyfile. Your config uses only one logger for each of your sites, using a snippet.

But that said, yes, using a snippet with args like that is a good way to reduce the number of lines in your config.

OK. Thanks for the clarification.

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