Rails Log File Rotation

Posted By Weston Ganger

I found that I had some giant log files that were taking up way to much space. What I needed was log rotation.

You can do this through Linux or through your rails app, I think in the rails app is easier.

Add the following line to config/application.rb

# config/application.rb
size = 5*1024 #5MB

rotated_logger = Logger.new(config.paths['log'].first, 5, size)

config.logger = rotated_logger
### OR recommended
config.logger = ActiveSupport::TaggedLogging.new(rotated_logger)

This tells it to have a maximum of 5 log rotated log files each with a maximum filesize of 5MB.

Also if your production environment runs as root (which it shouldn't) you may possibly get permission errors and have to resort to the built in linux methods.

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:September 17, 2015