How to setup log rotation for Rails apps

September 12, 2011

I’m sure we’ve all been there. You’ve been tasked with fixing a Rails app, you go in for some debugging magic and find the production.log is 3GB and counting as nobody bothered to setup log rotation. Here is how to avoid that…

On Ubuntu machines (and I would guess most other Linux distributions) logrotate comes installed by default. To set it up, you just need to create a new config file in /etc/logrotate.d/, for example:

$ cat /etc/logrotate.d/tweetedlinks-backend 
/var/www/TweetedLinks/current/log/*.log {
    rotate 90

This tells it to rotate any logs with the .log extension in /var/www/TweetedLinks/current/log/. You don’t need to worry about permissions or restarting anything, just plonk that in a file and relax. Job done.

In case you were wondering, here are what all the options mean:

  • compress – Compress rotated logs. zgrep and zcat are your friends.
  • copytruncate – Copy the log out of the way, and then truncate the existing logs. Some processes don’t handle log files being rotated, and will write logs to an empty file descriptor rather than the new log. This solves that.
  • daily – Rotate logs daily.
  • dateext – Add the date the logs were rotated as the extension rather than a number.
  • delaycompress – Skip compressing the log until the following day. Apparently some processes don’t handle logs being rotated properly and will write to the wrong file descriptor – n.b. does this even have any effect with copytruncate?
  • missingok – Don’t raise an error when there is a missing log file.
  • rotate 90 – Keep up to 90 days worth of logs. There doesn’t appear to be an option to not delete old logs, but you can just set this to a large number, like 36500 to keep 100 years of logs.

Discuss on Hacker News