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

hello all, this is my first real perl program you could say, and I was looking for suggestions/advice on it. I know there are other apache log rotaters out there but this one is mine :) It basically just takes the log renames it with a timestamp and gzips it the gracefully restarts apache. I was wondering if there was a better way to get a timestamp than to use a module, I mean I could manipulate localtime but that just seems like far too much annoyance.
#!/usr/bin/perl -w #Script to roll log files over nightly and HUP httpd use Date::Calc qw(Today); #function declarations sub graceful(); ($year,$month,$day) = Today(); $logdir = "/var/log"; $logname = "httpd-access.log"; $timestamp = "$year$month$day"; $apachectl = "/usr/local/sbin/apachectl"; $gzip = "/usr/bin/gzip"; if (-e "$logdir/$logname" && !-e "$logdir/$logname.$timestamp") { rename("$logdir/$logname", "$logdir/$logname.$timestamp"); graceful(); } else { print "Cannot rotate log to pre-existing timestamp\n"; } if (-e "$logdir/$logname.$timestamp" && !-e "$logdir/$logname.$timesta +mp.gz") { system("$gzip $logdir/$logname.$timestamp"); } else { print "file already exists"; } sub graceful() { system("$apachectl graceful"); #restart apache gracefully }

Replies are listed 'Best First'.
Re: suggestions for log rotater...
by Masem (Monsignor) on Nov 05, 2001 at 06:13 UTC
    If all you want is the day, month, and year, you can use the builtin localtime to get this information.
    my ( $day, $mon, $year ) = (localtime(time))[3,4,5]; my $timestamp = sprintf( "%04d%02d%02d", $year, $mon, $day ); #continue...

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      You may want to $mon++ since $mon goes from 0 to 11 rather than 1 to 12.

      Hope this helps :-)

        And make sure to add 1900 to the year...

        use strict; use Time::localtime; my $mCurrentTime; my $mTimeStamp; $mCurrentTime = localtime; $mTimeStamp = sprintf( '%04d%02d%02d', ($mCurrentTime->year)+1900, ($mCurrentTime->mon)+1, $mCurrentTime->mday );
Re: suggestions for log rotater...
by Aighearach (Initiate) on Nov 05, 2001 at 06:26 UTC
    You're not using strict mode, so probably I shouldn't comment... but...

    One way to do it with localtime instead of the module, in the same number of lines, is:

    my ($year, $month, $day) = (localtime)[5,4,3]; $timestamp = sprintf "%04i%02i%02i",$year+1900,$month,$day;
    One question is, does the Today() function return numbers, or strings with zero padding at the front? If they are numbers, you'll want to use a sprintf to zero pad it, or else use a glue character:
    $timestamp = join('.',$year,$month,$day);

    --
    Snazzy tagline here
Re: suggestions for log rotater...
by kschwab (Vicar) on Nov 05, 2001 at 06:20 UTC
    There's always POSIX::strftime. It's a module, yes, but part of the perl distribution.

    That's supposing your needs are beyond localtime.

Re: suggestions for log rotater...
by orbadelic (Acolyte) on Nov 05, 2001 at 06:54 UTC
    thanks for the comments, I keep forgetting that I should always : use strict; the Today() function returns numbers, not strings with zero padding. once again thanks alot for all the comments!