Setting Up Log Rotation for Ruby on Rails Application on Ubuntu

Once you've deployed a Ruby on Rails web application, chances are the log files grew in sizes and occupy most of the disk space. On a live application, often time the log size grew to Gigabytes in size and the server crashes because there is no disk space left. So before this happens, it is important to set up log rotation for your Ruby on Rails application.

This guide assumes that you're running flavors of Linux operating system as your server.

First of all, you want to check if logrotate is installed in the system. Most Linux system has this preinstalled. Run:

logrotate -v  

You should see the version of logrotate installed in the system. Good, we're ready to set up log rotation for our Ruby on Rails application.

The next thing to do is find out the folder where you set up the Ruby on Rails application. In our case, we put our application in /var/deploy folder and deploy using capistrano. When deploying using capistrano the log files are usually located in shared/log folder inside the application folder.

So heads to /etc/logrotate.d/ as a superuser and create the following file and name it as your application name. For example, our application name is pensil so we create a file /etc/logrotate.d/pensil:

/var/deploy/<appname>/shared/log/*.log {
  su deploy deploy
  create 0600 deploy deploy
  daily
  size 500M
  missingok
  rotate 3
  compress
  delaycompress
  notifempty
  copytruncate
}

You may want to replace <appname> with your application name. Let us go through each configuration one by one:

  • su deploy deploy: this means that user deploy and group deploy will be used to rotate log files.
  • create 0600 deploy deploy: this means that the created log rotation file will belong to user deploy and group deploy and only has read write permission.
  • daily: this means that logs are rotated daily
  • size 500M: this means that if log size is > 500MB then it will be rotated regardless of the daily options. For example, if the log size reaches 500MB before 24 hours since the last daily rotation, the log file will be rotated.
  • missingok: if the log file is missing, go on to the next one without issuing error message
  • rotate 3: the number of log rotations before logs are being deleted. Coupled with daily option, this means keep 3 days worth of log.
  • compress: compress old log files using gzip
  • delaycompress: this means that the log is compressed the second time it's rotated (not the first). Note that to use delaycompress, compress option needs to be present as well.
  • notifempty: Do not rotate the log if it is empty
  • copytruncate: Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. This option is very important to Rails application because the Rails server may still be running when log rotation occurs. Without this option, the Rails server will still write to the old log file and not freeing disk space.

To test:

logrotate --force /etc/logrotate.d/<appname>  

Again, you want to replace <appname> with your application name.

Conclusion

In this guide, we first check logrotate version in the Linux system. We then go ahead and create a log rotation configuration file to setup log rotation for our Ruby on Rails application. Then, we discuss options that we put on the log rotation configuration file. Finally, we test the log rotation configuration by forcefully running log rotation.