I want to check the size of a logfile and if it exceeds 10KB :
1)I need to move to [logfile_time_pid.log] .
2)Truncate backup logfile (remove top lines until size of logfile is less than 10KB)
3)Write them back to backup logfile.
4)Create a fresh new logfile.
Here is the code I wrote:
my $MaxLogSize=10240 if ! $MaxLogSize;
if( -s "$LOGFILE" > $MaxLogSize ) {
my $logbkp_name = time . "_" . $$; # backup logname
my $OLD = $LOGFILE; # storing originial logname in a new variable
$LOGFILE =~ s/\.log$/\_$logbkp_name\.log/; #substitute original log name with backuplog name
rename($OLD,$LOGFILE); #move original file to backup file
my @lines = qx(tail -c 10240 $LOGFILE); #get the 10KB size lines removing toplines
open(OUT,">$LOGFILE") or die "$!\n"; #recreate backup file
foreach (@lines)
{
print OUT $_ ; #write the lines to backup file
}
close(OUT);
open(FH,">$OLD") or die "$!\n"; create a fresh original logfile
}
if( -s "$LOGFILE" > $MaxLogSize ) {
my $logbkp_name = time . "_" . $$; # backup logname
my $OLD = $LOGFILE; # storing originial logname in a new variable
$LOGFILE =~ s/\.log$/\_$logbkp_name\.log/; #substitute original log name with backuplog name
rename($OLD,$LOGFILE); #move original file to backup file
my @lines = qx(tail -c 10240 $LOGFILE); #get the 10KB size lines removing toplines
open(OUT,">$LOGFILE") or die "$!\n"; #recreate backup file
foreach (@lines)
{
print OUT $_ ; #write the lines to backup file
}
close(OUT);
open(FH,">$OLD") or die "$!\n"; create a fresh original logfile
}
No comments:
Post a Comment