Activate Logrotation for AccessLog
https://www.jagsiacs.co.uk/2023/09/15/traefik-log-rotation/
Introduction
This post is about enabling log rotation for the Traefik access.log
. Some information is sourced from this post, with additional insights by Aeoneros.
Warning: Log Rotation
Traefik will close and reopen its log files, assuming they’re configured, upon receiving a USR1
signal. This enables the logs to be rotated and processed by external programs like logrotate
. However, this does not work on Windows due to the lack of USR signals.
The access.log
requires a different setup compared to the standard Traefik logs. For more details, refer to this post. Below are the steps to manually create log rotation for the access.log
.
Step 1: Configure the Path of the Access.log
In your Traefik static configuration file (e.g., traefik.toml
), define the file path for the access.log
. For example:
[accessLog]
filePath = "/mnt/glustermount/data/traefik_data/access.log"
Refer to the official documentation here.
Step 2: Install Logrotate
Ensure the logrotate
package is installed on your Debian-based system. Install it if necessary:
sudo apt update
sudo apt install logrotate
Step 3: Create a Logrotate File
Create a new configuration file for Traefik log rotation:
sudo nano /etc/logrotate.d/traefik
Step 4: Configure Logrotate
Paste the following content into the file:
/mnt/glustermount/data/traefik_data/access.log {
compress
create 0640 root root
daily
delaycompress
missingok
notifempty
rotate 5
postrotate
# Send USR1 signal to reopen logs
docker kill --signal=USR1 $(docker ps --filter "name=traefik_traefik" --format "{{.ID}}")
# Pause to give Traefik time to process the signal
sleep 5
# Verify that Traefik is writing to the correct log file
if [ -s /mnt/glustermount/data/traefik_data/access.log.1 ]; then
# Restart service if still writing to the rotated file
docker service update --force traefik_traefik
fi
endscript
}
Explanation:
- The
postrotate
script first sends aUSR1
signal to Traefik to reopen its log files. - If Traefik continues writing to the rotated file, it restarts the container using
docker service update --force
. - This ensures that logs are properly rotated and written to the correct file.
Step 5: Debugging Logrotate
Test your logrotate
configuration in debug mode:
sudo logrotate -d /etc/logrotate.d/traefik
Step 6: Force Logrotate
Force log rotation to execute the script:
sudo logrotate -f /etc/logrotate.d/traefik
Step 7: Verify Log Rotation
Check if new files were created and if Traefik is writing to the correct log file:
ls -lh /mnt/glustermount/data/traefik_data/access.log*
tail -f /mnt/glustermount/data/traefik_data/access.log
Troubleshooting
If nothing is working, revert the changes by following these steps:
- Rename or delete the logrotate file:
sudo mv /etc/logrotate.d/traefik /etc/logrotate.d/traefik.bak
- Edit the logrotate status file to remove entries related to the
access.log
:
sudo nano /var/lib/logrotate/status
- Look for and remove entries related to
/mnt/glustermount/data/traefik_data/access.log
.
Reapply the steps once the issue is resolved.