Neonode.cc - Mind space blog
open main menu
Part of series: caddy

Log Rotation in Caddy

/ 2 min read

Log Rotation in Caddy

Log rotation is the process of automatically archiving and deleting old logs to prevent disk space from filling up. For Caddy, log rotation can be set up using external tools like logrotate, widely used for log rotation on Linux.

Configuring Log Rotation with logrotate

1. Install logrotate (if not already installed)

sudo apt install logrotate

2. Create a Configuration for Caddy

Open or create a configuration file for Caddy in /etc/logrotate.d/caddy:

sudo nano /etc/logrotate.d/caddy

3. Sample Configuration for Caddy Log Rotation

Insert the following configuration into the file:

/var/log/caddy/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 root root  
    postrotate
        systemctl reload caddy
    endscript
}

If you are using a user other than root, replace it accordingly.

Explanation of the Settings

  • daily: Logs will be rotated daily. You can change this to weekly for weekly rotation.
  • rotate 14: Retain up to 14 log archives.
  • compress: Compress archives to save space.
  • delaycompress: Delay compression until the next rotation.
  • notifempty: Skip rotation if the log file is empty.
  • create 0640 root root: Create a new file with 0640 permissions and root as the owner.
  • postrotate: After rotation, restart the Caddy service to start writing to a new file.

Checking logrotate Configuration

You can check the logrotate configuration with the following command:

sudo logrotate -d /etc/logrotate.d/caddy

This command performs a test and shows how the rotation will proceed, without making changes.

Manually Running logrotate for Testing

To manually trigger rotation and verify functionality, run:

sudo logrotate -f /etc/logrotate.d/caddy

After these steps, logrotate will automatically rotate Caddy logs according to the specified settings.