Log rotation with Rails and cronolog

Some problems doing log rotation on Rails’ production.log:

  • The built-in rotation feature of the Logger class doesn’t play nice with concurrent fastcgi processes
  • Rails doesn’t appear to support the more advanced Log4r library, despite claiming to in a few places. In particular benchmarking seems to break it
  • Using logrotate proves annoying as you need to kill the fastcgi processes after rotating the logs

The solution appears to be relatively simple though - just use the standard logger, but pass it a pipe to cronolog rather than a filename. Don’t know why this wasn’t documented anywhere! So in environment.rb, or environments/production.rb say, something like:

# Use cronolog for log rotation
cronolog_io = IO.popen(
    'cronolog /space/log/%Y/%m/%d/production.log', 'w')
config.logger = Logger.new(cronolog_io)

6 Responses to “Log rotation with Rails and cronolog”


  1. 1 goncalo

    truly the pipe is superior because it has it’s own queue system so it keeps the sync all on the black box.

    you will still have problems with the number of writes (is logger a singleton?) my opinion is that hopefully the singleton will have a buffer which has a synced write to the buffer, and on a given interval or buffer size it will dump to the log file.

  2. 2 charlie

    afaik - pipes will lock the FD (and introduce stack wait), and we are writing with multiple processes. however, they are simple to understand.

  3. 3 matthew

    OK. yeah it doesn’t seem to be buffering the writes much if at all so i’m guessing cronolog is waiting for locks on the logfile fairly often.

    would be nice if cronolog had a configurable buffer size for stuff like this. doesn’t seem like a big bottleneck at the moment though.

  4. 4 goncalo

    sorry, i was just reading my comment and it was a bit confusing.

    yeah exactly - pipes introduce lock and wait on the descriptor but there is a chance this might develop to a bottleneck due to disk access time.

  5. 5 admin

    man this email-when-commented thing rocks, doesn’t it?

  6. 6 goncalo

    yeah !

    ?

Leave a Reply

You must login to post a comment.