Reducing log size

C

Cosmic Cruizer

I searched, but could not find suggestions on reducing the size of log
files on Windows. Truncate did not work since it keeps to top portion of
the log. I did not try to use tail.

Now that I wrote something that does what I want... what is a cleaner way
of doing this?

### Manage logfile size ###
sub getLogFile {
use File::Copy "move";
my $max_line_count = 50000; # Number of lines for replaced file
my $max_file_size = 3000000; # Maximum logfile size
my $size = (stat("$log"))[7]; # Get current logfile size
my $x; # Incrementer for counter

if($size > $max_file_size) {
# Get the number of lines in the logfile
open my $in, '<', $log or die "cannot open $log: $!";
1 while <$in>;
my $lines = $.;
close $in;

# Number of preceding lines to ignore (not copy)
my $target_size = $lines - $max_line_count;

# Open to read old log and write new temp log
open my $out, '>>', "TEMP.txt" or die "cannot open TEMP.txt: $!";
open my $in, '<', $log or die "cannot open $log: $!";
while (<$in>){
$x+=1;
print $out $_ if($x > $target_size);
}
print $out "\n$time_stamp\tFile size truncated\n\n";
close $in;
close $out;

# Replace old logfile and clean up temp
unlink $log;
move ("TEMP.txt", "$log");
unlink "TEMP.txt";
}
}


Thanks.
 
T

Tad J McClellan

Cosmic Cruizer said:
my $max_file_size = 3000000; # Maximum logfile size


Write is so that you don't get fingerprints on the screen counting digits:

my $max_file_size = 3_000_000; # Maximum logfile size

my $size = (stat("$log"))[7]; # Get current logfile size


my $size = -s $log;


See also:

perldoc -q vars

What's wrong with always quoting "$vars"?


unlink $log;


You should check the return value to see if you actually got
what you asked for...
 
X

Xho Jingleheimerschmidt

Cosmic said:
I searched, but could not find suggestions on reducing the size of log
files on Windows. Truncate did not work since it keeps to top portion of
the log. I did not try to use tail.

Why not?
Now that I wrote something that does what I want... what is a cleaner way
of doing this?

Using tail. Or rotating your logs more often, then deleting some of the
older ones.

Or can't you cleanly rotate logs on Windows?
while (<$in>){
$x+=1;
print $out $_ if($x > $target_size);
}
print $out "\n$time_stamp\tFile size truncated\n\n";
close $in;
close $out;

# Replace old logfile and clean up temp
unlink $log;

What happened to data that get written into the logfile after the while
finished but before the unlink happened?
move ("TEMP.txt", "$log");

What happened to process that tried to open $log during it's period of
nonexistence?
unlink "TEMP.txt";

Does move leave a copy of that behind?


Xho
 
C

Cosmic Cruizer

Ben, thanks for the suggestions. I made several of the changes both you and
Tad suggested. By using $. for the counter and removing several other
superfluous bits of code, it looks much better.

I'm surprised at how fast the code works, even on a 500 mb logfile.

There is one comment I'm unsure of... you asked "Why open for append?" Is
there a more effecient way to write to a file?

Thanks,

....Cos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top