Ruby on Rails
DeploymentTips

Log files

Your rails application will quickly accumulate large log files once in production. Ruby’s Logger class can be configured to roll them over for you, but this will cause errors if you have more than one Rails process (as most production sites do). See Agile Development with Rails (p.467 or p.628 in 2nd ed.).

The better solution is to write an external script to handle the rotation.

For example, on linux at least, there is logrotate which can take care of rotating your log files.


# Rails logs:
  /path/to/your/app/log/*.log {
  daily
  missingok
  rotate 14
  compress
  delaycompress
  notifempty
  copytruncate
  create 0666 www-data www-data
}

This will rotate 14 days worth of logs, compressing the older logfiles, and creating a new blank log writable by the web server.

Added “copytruncate” as rails doesn’t seem to be opening the new file, but rather keeps appending to the renamed one.

You don’t need the “create” line if you are using copytruncate. Logrotate ignores create if copytruncate is used.

Another solution is to move/rename the log files to an archive location and then send a USR2 signal to your FCGI processes:


mv /path/to/shared/log/production.log /path/to/archive

set kill_list=`ps waxo pid,command | grep dispatch.fcgi | grep -v grep | awk '{print $1}'`
kill -USR2 $kill_list