Rotating Rails Production Logs with LogRotate
Tags: rails, technology, logrotate, logrotateinrails
Published: 2021-01-28 13:54:32

---
title: Rotating Rails Production Logs with LogRotate
published: true
date: 2021-01-22 05:27:26 UTC
tags: RubyonRails,Technology,LogRotate,LogRotateinRails
canonical_url: https://www.botreetechnologies.com/blog/rotate-log-files-in-rails-application-with-logrotate-in-ubuntu/
---

![Log Files in Rails Application](https://www.botreetechnologies.com/blog/wp-content/uploads/2021/01/logrotate-in-rails-application.png)

We all know that when you hire [Ruby on Rails web development services](https://www.botreetechnologies.com/ruby-on-rails-development) 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](https://linux.die.net/man/8/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](https://www.botreetechnologies.com/blog/wp-content/uploads/2021/01/your_application_name.png)

###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](https://www.botreetechnologies.com/hire-ruby-on-rails-developers) can use based on their needs. You will find all options [here](https://linux.die.net/man/8/logrotate).

The post [Rotate Log Files in Rails Application with LogRotate in Ubuntu](https://www.botreetechnologies.com/blog/rotate-log-files-in-rails-application-with-logrotate-in-ubuntu/) appeared first on [BoTree Technologies](https://www.botreetechnologies.com/blog).