Limit the size of your Rails test and development logs

Thoughts from the team
By    | April 23, 2015 | Tips & Tricks,

I just ran out of disk space and went looking for things to delete, I was amazed to find nearly 10Gb of test.log and development.log files kicking around.

“Surely there must be a way to limit the size” I thought. There is!  And it’s easy.  And you should definitely do this now.

In your config/environments/test.rb and development.rb just add the line:

config.logger = ActiveSupport::Logger.new(
                     config.paths['log'].first, 1, 50 * 1024 * 1024)

and your log files will never grow bigger than 50Mb. You can change the size to your own preference. The ‘1’ in the second parameter means that 1 historic log file will be kept, so you’ll have up to 100Mb of logs – the current log and the previous chunk of 50Mb.

This works on Rails 4. You can find out more about the options in the Ruby Logger.new documentation.

Update: I’ve updated the code in this example from Logger.new to ActiveSupport::Logger.new to maintain the normal Rails log output format.

More like this