The default behaviour for a Rails application is to keep writing to the same log file until the entire disk of a server is filled. This is often undesirable on a production server. So here is how to rotate the log file on a Linux server.

The steps

Create a configuration file for the logrotate utility in /etc/lograte.d/ directory with the applications name.

vim /etc/lograte.d/cogar

Here is a recommended configuration:

/var/www/cogar/log/production.log {
    rotate 7
    size 10M   
    create 640 cogar_user cogar_user

Configuration explanation

I’ll explain what this does.

  • Line 1 is the location of the log file.
  • “daily” means rotate Daily, you can also say “weekly” or “hourly”
  • “missingok” means don’t freak out if the log is missing
  • rotate 7 means keep only the most recent 7 rotations.
  • compress means gzip the rotated files
  • delaycompress means DO NOT gzip the most recent, ie production.log.1
  • notifempty means do not rotate an empty log file
  • size 10M means do not rotate until the log reaches at least 10M in size, it will still run and check daily
  • the create line defines the owner of the rotated files and the umask