Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Rails Rotating Log Files with logrotate

with one comment

I know there’s a way to specify Rails log rotation parameters directly in the app. This works for some people:

# Can place this in environment.rb
# 2nd argument - number of log files to keep
# 3rd argument - size (bytes) that log files are allowed to reach before rotation
config.logger =, 8, 1024)

However…. I like the customizability of using logrotate better!
Here’s my logrotate config file that handles weekly log rotation, delayed compression and uses the copy-truncate method:

I place this config in the /etc/logrotate.d folder (ubuntu)
(ie. /etc/logrotate.d/<rails_app_name>)

/var/www/rails//shared/log/production.log {
  rotate 8

This config will rotate my production.log file weekly, keeping at most 8 log files. It delays compression until next rotation (extra precaution, simply to make sure the log file is not in use), and uses the ‘copytruncate’ method which basically copies the current log file, and then truncates this log file, so the Rails app maintains file pointer for continued writing.

Written by Andrew Waage

June 15th, 2011 at 11:56 am

One Response to 'Rails Rotating Log Files with logrotate'

Subscribe to comments with RSS or TrackBack to 'Rails Rotating Log Files with logrotate'.

  1. Thanks for posting this Andrew. I configured the same on my production box. Now, how do we test to make sure this works with out waiting a week to see the log rotation worked or not?


    23 Oct 11 at 3:37 pm

Leave a Reply