I will sometimes try to reinvent the wheel. Here is my effort. I wrote this several years ago, before I learned to love CPAN and stop reinventing the wheel. I added the syslog piece recently when I was testing Sys::Syslog for inclusion in other programs I am working on. A disclaimer - I do not use this in production.
package MyMods::Log::Logroll; use strict; use warnings; use Sys::Syslog; use Carp; our $VERSION = '0.01'; # Syslogging... my $slog = \&do_syslog; my %config = (); sub new { %config = @_; confess "Baselog not defined" unless $config{'baselog'}; $config{'maxbytes'} = 102400 unless $config{'maxbytes'}; $config{'maxlogs'} = 5 unless $config{'maxlogs'}; $config{'UID'} = ( exists $config{'strict'} == 1 ) ? $< : $>; #return ( &rollit() == 0 ) ? 0 : 1; } sub roll { my $logowner; my $filebytes; my $last = $config{'maxlogs'}; if ( -f $config{'baselog'} and ! -x $config{'baselog'} ) { # 0 dev device number of filesystem # 1 ino inode number # 2 mode file mode (type and permissions) # 3 nlink number of (hard) links to the file # 4 uid numeric user ID of file's owner # 5 gid numeric group ID of file's owner # 6 rdev the device identifier (special files only) # 7 size total size of file, in bytes # 8 atime last access time in seconds since the epoch # 9 mtime last modify time in seconds since the epoch # 10 ctime inode change time in seconds since the epoch (*) # 11 blksize preferred block size for file system I/O # 12 blocks actual number of blocks allocated my @info = stat($config{'baselog'}); $logowner = $info[4]; $filebytes = $info[7]; } else { $slog->("$config{'baselog'} is NOT a regular file"); return 1; } # File has not reached $maxbytes yet. return 0 if $config{'maxbytes'} > $filebytes; # obviously root can do anything if ( ! $config{'UID'} == $logowner and ! $config{'UID'} == 0 ) { $slog->("uid $config{'UID'} is trying to logroll $config{'base +log'} (owner is $logowner)"); return 1; } while ( $last > 1 ) { $last--; $slog->("renaming $config{'baselog'}"); rename_logs($last); } if ( -e $config{'baselog'} ) { rename( "$config{'baselog'}", "${config{'baselog'}}_1" ); $slog->("Error renaming $config{'baselog'} -> ${config{'baselo +g'}}_1") unless -e "${config{'baselog'}}_1"; } return ( touchfile() == 0 ) ? 0 : 1; } sub touchfile { my $BASELOG; unless ( open $BASELOG, ">$config{'baselog'}" ) { $slog->("Unable to open $config{'baselog'}"); return 1; } return ( close $BASELOG ) ? 0 : 1; } sub rename_logs { my $x = shift; my $y = $x + 1; if ( -e "${config{'baselog'}}_$x" ) { $slog->("renaming ${config{'baselog'}}_$x -> ${config{'baselog +'}}_$y"); rename("${config{'baselog'}}_$x", "${config{'baselog'}}_$y"); my $return = ( -e "${config{'baselog'}}_$y" ) ? 0 : 1; $slog->("Error renaming ${config{'baselog'}}_$x -> ${config{'b +aselog'}}_$y") unless $return == 0; return $return; } } sub do_syslog { my $message = shift; openlog( $0, "ndelay,pid", "local0"); syslog("info", $message); closelog; } 1;
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

In reply to Re^3: Log Rotation by tcf03
in thread Log Rotation by Danikar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.