Log Files Rotation in Ruby on Rails Application with LogRotate in Ubuntu

logrotate installation in ubuntu
  • 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,
  • 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.
  • Run the following commands for checking logrotation.
  • -d (debug mode) : no changes will be made to logs or the 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.
  • 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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



Leading software development company in the USA, Singapore & India. Hire developers to build web apps with expert software development services.