Ruby on Rails production log rotation

cnicolaou picture cnicolaou · Feb 3, 2011 · Viewed 65.1k times · Source

What is the best way to enable log rotation on a Ruby on Rails production app?

Is it by using logrotate on the hosting server or is there a set of options to use when initialising logger from the app?

Answer

berkes picture berkes · Feb 3, 2011

Option 1: syslog + logrotate

You can configure rails, to use the systems log tools.

An example in config/environments/production.rb.

# Use a different logger for distributed setups
config.logger = SyslogLogger.new

That way, you log to syslog, and can use default logrotate tools to rotate the logs.

Option 2: normal Rails logs + logrotate

Another option is to simply configure logrotate to pick up the logs left by rails. On Ubuntu and Debian that would be, for example, in a file called /etc/logrotate.d/rails_example_com.

/path/to/rails.example.com/tmp/log/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    copytruncate
}

As per suggestions below, in Rails it is advised to use copytruncate, to avoid having to restart the Rails app.

Edit: removed "sharedscripts/endscript" since they are not used here and cause problems according to comment. And removed create 640 root adm as per comment suggested.