Log rotation/clearing in Ruby on Rails

logging, ruby-on-rails

Solution

The ruby logger is on hand to help you out here - and it has default options for rotation.

Here's what I do:

In `environment.rb` we define our own logger

new_logger = Logger.new(File.join(RAILS_ROOT, "log", "new_logger_#{RAILS_ENV}.log"), 'daily')
new_logger.formatter = Logger::Formatter.new

This creates our own loggers... with a formatter (so you get timestamps etc), with one per environment, and rotated daily.

Then in the initialization block we ask Rails to use this logger

Rails::Initializer.run do |config|

  config.active_record.logger = new_logger
  config.action_controller.logger = new_logger

  #snip
end

You can obviously see the power here too to have different loggers for `active_record` and for `action_controller` - sometimes very useful!

Problem

How can I setup the automatic cleanup on test.log and development.log in ruby on rails? Is there a setting to automatically delete dev and test logs on server start and tests run?

Original source