Bryan Lee

Rotating Your Rails Logs In Production

· Ruby on Rails

I encountered a really amateur issue that I think shouldn't be happening after so many years working in Rails - my Rails log ballooned into a Godzilla proportions and I ran out of disk space. Admittedly, I've been too spoilt by one-click deploy solutions like Heroku, Opsworks for so long and only recently moved to a Capistrano + VPS deploy solution that just handle everything for you.

If you're reading this, you either have neglected to rotate your Rails logs and ran out of disk space too (welcome to the club), or you just stumbled upon this. Anyhow, Rails (and Ruby) has made it really simple to rotate your logs.

The Rails way, is to modify this line in your config/production.rb.

config.logger =["log"].first, "weekly")

You can also keep split your logs daily, and keep the files for n number of days:

config.logger =["log"].first, 7, "daily")

This works by utilising Ruby's built in Logger class. An alternate solution is to have your Linux OS do it using logrotate. There are specific instructions for each flavor of Linux and I won't go too much into specifics. My preference is to using Rails, it is a convenient 'code' approach which is much more portable (no need to configure logrotate).