Symfony log management with `logrotate` on Linux

It sucks when server disks fill up. I have Nagios set up to notify me well before it’s a problem, but it happens annoyingly frequently, and it’s always the same cause: log files.

Symfony’s production environments don’t create logs, but the development environments do, and for one project I had several batch jobs logging to the myproject/log directory. (Troubleshooting batch jobs without logs is just crazy).

I was going to use symfony log:rotate, but

  • I would need one cron job for each log
  • I had permission issues since some of the logs belong to apache and others to root
  • Everything else on the machine, pretty much, is managed with the standard Linux utility logrotate. Logrotate has been around for about 174 Linux Years, and is common to most distributions.

So here’s how I did it using with logrotate.

  1. WARNING: You might want to make a convenient copy of your log directory before you do this. I accidently wiped all the logs out while messing with it.
  2. Create a file config/myproject.logrotate. Very simple, I just want it to use the same rotation schedule specified for the whole machine in /etc/logrotate.conf
    /path/to/myproject/log/*.log {
            rotate 4
  3. Check that it will do stuff by running it with the debug flag
    logrotate -df config/badge.logrotate
  4. Make a symlink to it in /etc/logrotate.d
    ln -s /path/to/myproject/config/myproject.logrotate  /etc/logrotate.d/myproject
  5. Run it the first time manually to rotate your logs and make sure it does the right thing!
    sudo logrotate -fv /etc/logrotate.d/myproject