Rotate Rails Logs with logrotate

This technique requires root shell access.

The logrotate program on linux servers handles rotation and compression of system logs. When logs get too big after being written to for a long time, logrotate will take the old log data and archive it somewhere else.

By default, logrotate only watches system logs which are usually located in /var/log/. To rotate Rails logs, a bit of configuration is needed.

Verify that the logrotate cron job (scheduled task) is enabled with the following:

$ ls /etc/cron.daily

Make sure logrotate is scheduled. The configuration file for logrotate is located at /etc/logrotate.conf. To enabled log rotation of Rails logs, open it up with your preferred text editor:

$ sudo vi /etc/logrotate.conf

At the end of this file, add the following lines (replacing the rails application path with your own path):

"/[path to rails application]/log/production.log" {
    compress
    olddir /[path to rails application]/log/old
    rotate 4
    size=5000k
    weekly
}

Make sure the path to the rails application is an absolute path, such as one that begins with the character /.

Here are explanations of the options that you can specify:

Some useful advanced configuration options:

To specify logrotate on more than one rails log, just add another block for the other log and so on.

To see which system logs have logrotation configured, use this command:

$ ls /etc/logrotate.d

For more information about log rotation and logrotate, use this commmand:

$ man logrotate