Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have any idea how I could create a new logfile every n minutes?

I'd like to base this on the current time but I don't really want to read in the time every x seconds.
Is there a better way? Thanks!

Replies are listed 'Best First'.
Re: Automating File Rotation
by borisz (Canon) on Sep 14, 2004 at 18:58 UTC
    use rotatlelogs that comes with your apache.
    Usage: rotatelogs <logfile> <rotation time in seconds> [offset minutes + from UTC] or <rotation size in megabytes>
    Boris
Re: Automating File Rotation
by ccn (Vicar) on Sep 14, 2004 at 18:45 UTC
      I checked that module out but it only increments the name of the logfile as specified by a counter value. I was hoping to rotate mine based on a time value

        So copy the module, rename it, add use POSIX qw(strftime);, remove

        for($i = $self->{'Count'}; $i > 1; $i--) { $j = $i - 1; $next = "${currn}." . $i . $ext; $prev = "${currn}." . $j . $ext; if ( -r $prev && -f $prev ) { move($prev,$next) ## move will attempt rename for us or croak "error: move failed: ($prev,$next)"; } }

        and change
        $next = "${currn}.1";
        to
        $next = "${currn}_" . strfmt('...', localtime);

        Here's some time formats friendly to Windows and UNIX shells which are string-sortable:

        use POSIX qw(strftime); @t = localtime; $\ = "\n"; print strftime("%Y%m%d%H%M%S", @t); # logfile_20040914151753 print strftime("%Y%m%d-%H%M%S", @t); # logfile_20040914-151753 print strftime("%Y-%m-%d-%H%M%S", @t); # logfile_2004-09-14-151753 print strftime("%Y-%m-%d@%H-%M-%S", @t); # logfile_2004-09-14@15-17-53
Re: Automating File Rotation
by gaal (Parson) on Sep 14, 2004 at 19:17 UTC
    On linux, try the logrotate package.
Re: Automating File Rotation
by sintadil (Pilgrim) on Sep 14, 2004 at 19:18 UTC

    Does anyone have any idea how I could create a new logfile every n minutes?

    Most decent Unix systems provide you with a newsyslog utility which runs from cron or a workalike which does exactly what you want, and often much more. Any decent implementation isn't limited to working with syslog logfiles.

    If you're running on Windows or something ne Unix ... well ... I honestly don't know. You'd need to post more about what sort of logfile you're rotating, any specifics about rotating it, etc. before we could be more detailed in our answers.

Re: Automating File Rotation
by ikegami (Pope) on Sep 14, 2004 at 19:43 UTC
    To "not really want to read in the time every x seconds" sounds like a unnecessary and valueless optimization to me, since you were talking about doing this at most every minute. In addition to that, if you want to use a formatted date -- and by formatted, I mean anything other than a number of seconds -- you have to figure out how to convert a number of seconds into year, month, ..., second.

    If you insist:

    our $SLEEP_SECS = 5; # Wake up every 5 seconds. our $GET_TIME_SECS = 2*60; # Resync $time every 2 minutes. my $time = time; my $sleep_count = 0; { ... do stuff that doesn't take too long ... sleep($SLEEP_SECS); if (++$sleep_count * $SLEEP_SECS >= $GET_TIME_SECS) { $time = time; $sleep_count = 0; } else { $time += $SLEEP_SECS; } redo; # loop back to start }

    That code falls apart if any signals wake up sleep()

Re: Automating File Rotation
by ranjan_jajodia (Monk) on Sep 15, 2004 at 14:01 UTC
    On linux you can use cron to change the logfilename. Use a softlink to point to the latest log. Every n minutes create a new file (via cron) and make the softlink to point to the new logfile.