January 29, 2007

Log rotation in Ruby

I just checked in a modification to the standard log object that changes a few things to allow for nice log rotation. First, log objects you create through Log.create now use a wrapper class for the webrick log object. You can still use all the methods on the logger object like .info, .debug etc. However, the log engine now keeps track of all created logfiles & when it recieves a SIGUSR1 on linux it will reopen all the log files:


jay@foo:/var/log/call_svc$ ls
call_svc-std.log.1 call_svc-std.log.2
jay@foo:/var/log/call_svc$ ps -ef | grep ruby
jay 4435 4003 0 11:59 pts/0 00:00:02 ruby bin/ragi_server.rb
jay@foo:/var/log/call_svc$ kill -SIGUSR1 4435
jay@foo:/var/log/call_svc$ ls
call_svc-std.log call_svc-std.log.1 call_svc-std.log.2


So this means we can now create logrotate scripts that don't require restarting any services:


/var/log/call_svc/call_svc-std.log {
    rotate 30
    daily
    postrotate
        /sbin/kill -SIGUSR1 `cat /snapvine/pids/call_svc`
    endscript

}


I'll try to find a moment to clean it up and post it...

-jay