Welcome Guest | Login

What is the best practice for rails log rotation? How to use logrotate?

From what I've read it seems that adding something like:

 config.logger = Logger.new(config.log_path, 50, 1048576)

to your environment is the easiest solution. But then I read that solution has problems if you are using multiple mongrel instances which I am. The more robust solution seems to be to use logrotate as described here:

  http://wiki.rubyonrails.org/rails/pages/DeploymentTips/

But I'm not sure how to set that up on hostingrails servers. Can someone tell me what the best way to rotate my logs on hostingrails is?

Thanks,
Jesse

2007-06-20 01:02 PM

Hmm - probably better than logrotate.conf is just to write up a quick shell script to take care of it and run a daily or weekly cron.  We'll see if we can post one up here in a bit...

2007-06-20 01:43 PM

OK - how about this -
#!/bin/sh
cd /home/username/path/to/rails/app/log
cp production.log /home/username/path/to/your/old/logs/production.log.`date +%Y%m%d`
cd /home/username/path/to/rails/app
/usr/local/bin/ruby rake log:clear
save that into a file called logrotate.sh in your root and make it chmod 755  

set a cron
0 1 * * * /bin/sh /home/username/logrotate.sh
if you only want your production.log cleared instead of the others, instead of the last two lines just put

cat /dev/null > production.log


(you may want to test this all out with a fake file to be sure)

2007-06-20 02:02 PM

Thanks I didn't know that rake could help. Also for other's reading this there is a typo in the above. It should be:

 "rake log:clear"

instead of "rake logs:clear", at least that's what I needed to do to get it working.

Thanks,
Jesse

2007-06-20 02:29 PM

Ahh yes, I went ahead and edited it - thanks.

2007-06-20 02:31 PM

This seems like a really good solution but do you have to kill the current fcgi processes like the DeploymentTips article suggests?

2007-06-23 08:48 PM

Hi there - no - you don't need to kill the fcgi processes (or Mongrel) - the production.log file never moves and thus emptying it won't disrupt your app.

2007-06-24 03:46 AM

I created a file and cron job as William suggested above, and the copy part worked, but the log file was not cleared.  I used the last two lines as originally suggested (not the cat /dev/null > production.log approach), as in:

cd /home/<myusername>/<projectdir>
/usr/local/bin/ruby rake log:clear

Am I missing something? (permissions, path names...)  I'm new to most of this...

2007-06-28 12:08 PM

Hi there - I don't think you're missing anything, just replace those last two lines with

cat /dev/null > production.log

and it should work.  How many mongrels (or fastcgi?) are you running?

2007-06-28 04:58 PM

cat /dev/null worked fine.  I can live with that...

BTW, I'm running just a single mongrel--if that means anything.

2007-06-29 06:02 PM

No, I don't think the number of mongrel instances have something to do with that.

2007-06-29 06:17 PM

Here is a slight modification of upper script we hope It will help someone. It archives last 6 months with tar.gz to save space.
http://pastie.org/297917

and here is a crontab entry:
@monthly  cd /path/to/rails_app/log && /path/to/script/logrotate.sh

2008-10-22 05:55 AM


Hello Guest! In order to post you must be an active client with us, please log in or sign up.



Previous URLs for this page here and here