Ruby on Rails production log rotation with LogRotate in Ubuntu

Feature thumb logrotate in rails application

We all know that when you hire Ruby on Rails web development services for a web app, the application server is processing the requests, it creates log files. Few weeks ago, one of my application server crashed several times. By looking into server logs, we found that log files consume too much space in the server.

However if the log files keep growing they can outrun the disk space and there might be chances of your application server crashes. Now how to resolve this issue ? Let’s see the solution I have preferred for Log Rotation.

Linux is providing a standard utility called Logrotate which has all the features you typically need!

Let’s see how to work with Logrotate library to rotate our Rails application’s log files.

Log Rotation setup is really easy. A bunch of your operating system is already using it, we just need to plug in our configuration and we are set! The first step is to open up the configuration file of your application under ‘/etc/logrotate.d’ folder by using vim or nano. e.g. => sudo nano your_application_name Now add the following content in the file you just created, Now add the following content in the file

Let’s see what each line actually means.

daily – Log files are rotated daily. missingok – if the log file is missing, go to the next one without issuing an error message. rotate 7 – log files are rotated 7 times before it will be deleted or mail to the address specified in mail option (you can specify mail option if you want to email the log files) compress – this is an important command, for old versions of log files, it will gzip the log files and saves the disk space. dateext – for older versions of log files, it will add date extension like YYYYMMDD instead of simply adding numbers. It will be easy to find every day log files by this command (you can also specify date format by adding dateformat command). delaycompress – this is a cool option used with compress. It delays the compression of rotated log files by one cycle. This is important when an application is not writing new logs immediately and continues writing to the old logs.Delaying compression would ensure the log messages are received in the rotated log file. copytruncate – this option is required unless you want to restart the rails application server after log rotation. Otherwise the application continues to log in the old log file. This option truncates the original log file after creating a copy. instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its log file and thus might continue writing (appending) to the previous log file forever.

There are so many  other options available in logrotate which Ruby on Rails developers can use based on their needs. You will find all options here.

call now ruby on rails web development

Now let’s see how to run log rotation to check that it is working properly.

Run the following commands for checking logrotation. -d (debug mode) :  no changes will be made to logs or the logrotate state file. logrotate state file -f (force mode) : it tells the logrotation to force the rotation. Sometimes it is useful after adding new entries to the log rotate configuration file to check it works properly. log rotate configuration file When you forcibly run the logrotate command, you will get the output zip file of your current log file or you can wait until the next day to run it automatically and generate a gzip file of your old log file. You will find the following output in a few days if your logrotate in rails is working properly. Note that we have added *.log in our configuration file, this is why all the logs file inside the log folder will be automatically rotating.

As we have mentioned daily option, the following output is of everyday rotation. You can specify weekly and monthly options also.

rotation options

Congratulations, you’re set!  Happy log rotating