Wednesday, August 19, 2009

How to Rotate Your Ruby on Rails Log Files

Nobody wants a disk to fill up when you least expect it so here is a simple step by step for rotating your Phusion Passenger log files.

1. Create a file called /etc/logrotate.d/passenger

And put the following contents in it.
/home/myapp/log/*.log {
daily
missingok
rotate 30
compress
delaycompress
sharedscripts
postrotate
touch /home/myapp/tmp/restart.txt
endscript
}

Now your logs will get rotated daily with the previous day being compressed into it's own file. The compressed files will sit around for 30 days then be deleted. After the rotation is done, postrotate, it will touch /home/myapp/tmp/restart.txt to restart passenger.

2. Test it

Run: logrotate -f /etc/logrotate.d/passenger

After running, you should see an archived log file and a fresh new active one taking the original files place. Also, Passenger will restart so it can log to the new file.

No comments:

Post a Comment