DEV Community

BoTree Technologies
BoTree Technologies

Posted on • Originally published at botreetechnologies.com on

Rotating Rails Production Logs with LogRotate

Log Files 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. A few weeks ago, one of my application servers 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

code

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 logfile 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.

The post Rotate Log Files in Rails Application with LogRotate in Ubuntu appeared first on BoTree Technologies.

Top comments (0)