ChrisCodes.com

Rotate Rails application logs with logrotate on Debian

Here's a logrotate script to keep your Rails application's logs from getting out of hand. This is from a Debian GNU/Linux box, but should be similar on other Linux systems, give or take a path or two.

Create a file in the /etc/logrotate.d directory, eg. /etc/logrotate.d/rails-apps, with the following

/rails-apps/*/log/*.log {
        weekly
        missingok
        copytruncate
        rotate 12
        compress
        notifempty
        sharedscripts
        postrotate
           if [ -f /var/run/lighttpd.pid ]; then \
             if [ -x /usr/sbin/invoke-rc.d ]; then \
                invoke-rc.d lighttpd force-reload > /dev/null; \
             else \
                /etc/init.d/lighttpd force-reload > /dev/null; \
             fi; \
           fi;
        endscript
}

This will run weekly, so if you want to test it to be sure it works first, just run:

sudo /usr/sbin/logrotate -f /etc/logrotate.d/rails-apps

The first line will look in each directory in /rails-apps for the log/ directory. It will rotate all files ending with .log, so if you store the rails logs, lighttpd (or Apache) access/error logs in your app's log directory, they're all taken care of.
This way multiple applications (and applications added in the future) are covered.

I'm using lighttpd, so after rotating the server is restarted

As with any Linux utility, logrotate is very configurable, so be sure and check man logrotate for all of the glorious options. Or check out one of the many articles and tutorials available

Chris keyed this in on: 2007-05-22
Filed in: Debian, Linux, Rails, Webserver

Feed Icon

Copyright © 2009 Chris Martin