How to Logrotate Ruby On Rails Application’s Logs On Linux Systems

undefined

In this post, I’ll show you how to successfully rotate your Ruby On Rails application’s logs using Logrotate on Linux systems. Log rotation is a very important process to save your servers disk space and guarantee a healthy working environment for Linux OS. This post is valid for both RPM Linux family ( Redhat /CentOS /Scientific Linux and Fedora ), and Debian Linux family ( Debian and Ubuntu ).

As a systems engineer, one of your roles is to rotate the logs produced by running ROR apps on your server periodically and  as a ROR developer, you might be enjoy building software and web applications but strongly dislike the server maintenance that comes with it, here’s, I’ll show you how to logrotate Rails production logs weekly to archive a copy of your logs and then compress them to save space and make them much more manageable.

Your log files can grow fast if you have a lot of traffic, especially access logs.  For my Apps, Nginx produces about 12 GB access logs monthly and Rails produces about 12 to 15 GB of logs monthly. Leaving those files without rotation is very very bad for your OS. You have two options to deal with those logs files:

  1. Periodically delete/empty those files manually, Not recommended
  2. Use Logrotate facility to automatically handle those logs files, which we’ll explain here.

Configuring Logrotate on ROR Applications

You might be surprised at just how easy to setup logrotate Rails logs is. The reason it is so handy is that a bunch of your operating system software is already using it. We just have to plug in our configuration and we’re set!

The first step is to create your ROR logrotate configuration file at /etc/logrotate.d/MY_APP which populated with the following block of code, using the following command:

# vim /etc/logrotate.d/<MY_APP>

Then add the following code into it and save.

/home/deployer/<MY_APP>/current/log/*.log {
 weekly
 missingok
 rotate 8 
 compress
 dateext
 delaycompress
 notifempty
 copytruncate
}

You need to set the logrotate configuration file name and to change the first line to match the location where your Rails app is deployed “replace <MY_APP> with your own”. Mine is under the deploy user’s home directory.

How It Works

This is fantastically easy. Each bit of the configuration does the following:

  • weekly – Rotate the log files each 7 days. You can also use daily or monthly here instead.
  • missingok – If the log file doesn’t exist, ignore it
  • rotate 8 – Only keep 8 days of logs around
  • compress – GZip the log file on rotation
  • dateext – Adding a date extension like YYYYMMDD instead of simply adding a number to the archived versions of log files
  • delaycompress – Rotate the file one day, then compress it the next day so we can be sure that it won’t interfere with the Rails server
  • notifempty – Don’t rotate the file if the logs are empty
  • copytruncate – Copy the log file and then empties it. This makes sure that the log file Rails is writing to always exists so you won’t get problems because the file does not actually change. If you don’t use this, you would need to restart your Rails application each time.

Running Logrotate

Since we just wrote this configuration, you’ll want to test it.

To run logrotate manually, run the following command:

#  logrotate /etc/logrotate.conf

You need to run it a second time to make sure the delay compress works.

Here’s the output after two weeks from enabling the logrotate on my production server:

# ls ~/MY_APP/current/log/ -lh
total 8.3G
-rw-r--r-- 1 deployer deployer 69 Mar 12 11:35 development.log
-rw-r--r-- 1 deployer deployer 181 Mar 5 07:25 development.log-20170305.gz
-rw-r--r-- 1 deployer deployer 69 Mar 12 07:25 development.log-20170312
-rw-r--r-- 1 deployer deployer 209M Mar 14 08:19 production.log
-rw-r--r-- 1 deployer deployer 13M Mar 5 07:25 production.log-20170305.gz
-rw-r--r-- 1 deployer deployer 760M Mar 12 07:25 production.log-20170312
-rw-r--r-- 1 deployer deployer 1.6G Mar 14 08:19 puma_access.log
-rw-r--r-- 1 deployer deployer 706M Mar 5 07:25 puma_access.log-20170305.gz
-rw-r--r-- 1 deployer deployer 5.0G Mar 12 07:26 puma_access.log-20170312
-rw-r--r-- 1 deployer deployer 127K Mar 14 07:39 puma_error.log
-rw-r--r-- 1 deployer deployer 1.7M Mar 5 07:25 puma_error.log-20170305.gz
-rw-r--r-- 1 deployer deployer 288K Mar 12 07:28 puma_error.log-20170312

You can see that the logs are rotated every a week and it compressed successfully, also My log files sizes are too big for keeping it on my server without handling. Once we get up to 8 log files, the next time logrotate runs, it will delete the oldest one so that we only have 8 days worth of logs. If you want to keep all the logs around, you can remove the rotate line from the configuration.

Hint:

1. Logrotate command runs daily and check it's existing configuration files, and rotate the logs.
2. The cron daemon is the one responsible for running the logrotate daily.

I hope this article is good enough for you.
See you in other articles.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *