11

How do I enable log rotation for log files e.g. access.log.

Is this built in ?

Docs only say "This allows the logs to be rotated and processed by an external program, such as logrotate"

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
Robert Lachner
  • 657
  • 1
  • 8
  • 22

3 Answers3

27

If you are running Traefik in a Docker container then you can do something like this:

Check that logrotate is installed on the Docker host:

logrotate --version

Create file in /etc/logrotate.d/:

vi /etc/logrotate.d/traefik

Put the following script, do not forget to fill with the container name.

/var/log/traefik/*.log {
  size 10M
  rotate 5
  missingok
  notifempty
  postrotate
    docker kill --signal="USR1" <container-name>
  endscript
}

Run!

logrotate /etc/logrotate.conf --debug 
logrotate /etc/logrotate.conf
CodeFox
  • 3,321
  • 1
  • 29
  • 41
Slipstream
  • 13,455
  • 3
  • 59
  • 45
  • 6
    This also works if Traefik is running outside Docker, in which case the line starting with `docker` should be adjusted. I configured Traefik as a Systemd service and set the command to `systemctl kill --signal="USR1" traefik`. – Ben Companjen Jan 07 '19 at 07:18
  • 1
    To me it didn't work with the quotes around the signal name, I had to remove them: `systemctl kill --signal=USR1 traefik`. I'm not sure why, but [it is used in this way](https://docs.docker.com/engine/reference/commandline/kill/#send-a-custom-signal--to-a-container) in the docker reference – Choma May 16 '20 at 16:52
9

It seems like there's no logrotation built-in so i enabled logrotation on the Host that traefik_access.log is mounted to.

In order for this to work when traefik is running in a docker container, you must volume mount the directory containing the log file (/opt/traefik/logs), not the log file itself (/traefik_access.log).

volumes:
  - /opt/traefik/logs:/logs

My logrotate-config for traefik 1.7.4 running in a container with volume mount to "/opt/traefik/logs":

/opt/traefik/logs/*.log {
  daily
  rotate 30
  missingok
  notifempty
  compress
  dateext
  dateformat .%Y-%m-%d
  create 0644 root root
  postrotate
  docker kill --signal="USR1" $(docker ps | grep traefik | awk '{print $1}')
  endscript
}
Vincent Doba
  • 4,343
  • 3
  • 22
  • 42
Robert Lachner
  • 657
  • 1
  • 8
  • 22
  • 1
    just mentioning to be mindful that other containers might also have a "traefik" component in their name - like error pages, etc. and the sub may return unexpected results for some – Mark Nov 08 '20 at 01:49
  • 1
    If you want to use the exact name, you could use `docker kill --signal="USR1" $(docker ps | grep '\btraefik\b' | awk '{print $1}')` - notice the `\b` before and after the container name. – Nepomuk Pajonk Jan 05 '21 at 06:17
7

Log Rotation

Traefik will close and reopen its log files, assuming they're configured, on receipt of a USR1 signal. This allows the logs to be rotated and processed by an external program, such as logrotate.

https://docs.traefik.io/v1.6/configuration/logs/#log-rotation

ldez
  • 3,030
  • 19
  • 22
  • Hi, I used this approach on a single docker host, using "docker-compose restart" inside logrotate's postrotate. But when in a swarm mode, the way to restart the service is using "docker service update --force traefik". In this scenario, the service is restarted in every host participating in the swarm, but the logs are not rotated on every host. Am i missing something? – HelLViS69 May 23 '18 at 08:02
  • 1
    "assuming they're configured, on receipt of a USR1 signal" i can't figure out what this means. So everytime logrotation rotates theres a new empty "access.log" file and logging is not visible anymore. What am i doing wrong ? – Robert Lachner Jun 05 '18 at 13:24
  • 1
    @RobertLachner I experienced this too. In my docker-compose.yml file, I was exporting the log files to the host system individually. When I stopped doing this and exported a directory instead (tell traefik to put the log files in here), the problem went away. Maybe this will help you. – anastymous Dec 01 '18 at 21:23
  • it works now for me with the config i mentioned earlier. Just added this config to "/etc/logrotate.d/traefik" It can be tested with: logrotate /etc/logrotate.d/traefik --debug – Robert Lachner Jul 22 '19 at 12:00