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:
- compress enables gzip compression for log files. This option seamlessly saves a lot of disk space because text files are highly compressible. To disable compression, use nocompress.
- olddir [dir] tells logrotate where to put the old logs.
- rotate [num] tells logrotate how many times a log is rotated before it is removed (or mailed, more on that later). If it is 0, old logs are deleted when logrotate runs.
- size=[size] tells logrotate how big log files should be before logrotate rotates them. You can give a number of bytes, or use the suffixes k for kilobytes and M for megabytes such as 300k or 10M.
- weekly tells logrotate to check every week to see if logs need to be rotated. Other options include monthly and daily.
Some useful advanced configuration options:
- copy tells logrotate to copy logs but not to clear the original log when the old logs are exported.
- mail [address] tells logrotate to email the old log to an email address before it deletes it. Logs will only be emailed (and deleted) after they have been rotated rotate number of times.
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