Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Log Rotation

by tcf03 (Deacon)
on Nov 27, 2007 at 16:08 UTC ( #653290=note: print w/replies, xml ) Need Help??


in reply to Re^2: Log Rotation
in thread Log Rotation

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://653290]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-05-18 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?