Rotating Mongrel Logs

May 6th, 2008

I am working on an Rails application using Apache and Mongrel on a Linux (Fedora Core) box. I want to be able to rotate the Rails logs to prevent them from taking up too much disk space. I found a number of blog posts / articles which had complicated(ish) directions to halt the mongrels, move (rename) log files, and then restart the mongrels. I knew there had to be a simpler way to handle this using the operating system. After much searching I ran into this blog post by Mike Mangino: Log Rotation with Mongrel.

I use Vlad the Deployer to deploy my app so my log files are in a shared/log folder in the root directory of the app. Following Mike’s directions, I created a file in ’/etc/logrotate.d’ with the following:


/PATH/TO/YOUR/APP/shared/log/LOGFILENAME {
  weekly
  rotate 8
  notifempty
  missingok
  compress
  copytruncate
}

Basically, the above tells logrotate to rotate the log file located at ’/PATH/TO/YOUR/APP/shared/log/LOGFILENAME’, rotate the logs weekly, keep 8 log files (I keep two months of logs), do not rotate if the log is empty, do not error out if a log file does not exist, compress the rotated log files, copy the log file to a new file and then truncate the original log file.

With Fedora, logrotate is a daily cron job (which checks each file in in the ’/etc/logrotate.d’ directory for rotation schedule) so there is no need to create a cron job or add a file to one of the cron directories (cron.hourly, cron.daily, cron.weekly, cron.monthly) though it is trivial to create a custom schedule for your log rotation. If you do not specify a rotation schedule (daily, weekly, monthly, yearly), logrotate uses a default of ‘weekly’.

For more information about logrotate, check out the online logrotate man page

Now you can painlessly rotate your mongrel logs!

 

Kenphused

Search

Categories

Archives

Tags