Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reducing log size

Reply
Thread Tools

Reducing log size

 
 
Cosmic Cruizer
Guest
Posts: n/a
 
      02-07-2009
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.
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      02-07-2009
Cosmic Cruizer <(E-Mail Removed)> wrote:

> 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...


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
Xho Jingleheimerschmidt
Guest
Posts: n/a
 
      02-07-2009
Cosmic Cruizer wrote:
> 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
 
Reply With Quote
 
Cosmic Cruizer
Guest
Posts: n/a
 
      02-08-2009
Ben Morrow <(E-Mail Removed)> wrote in
news:(E-Mail Removed):


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
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      02-08-2009
Cosmic Cruizer wrote:

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


500 millibits, why would you want to use a computer for that?

--
Ruud
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reducing size of avi file Stephen Fisher Computer Support 3 07-23-2005 03:36 PM
Reducing the size of a serialized object. Andoni Java 4 07-21-2005 03:59 PM
Reducing size of web page that contains jpg image Dan Shelby HTML 7 01-26-2005 04:50 AM
reducing picture size Tricia Computer Support 5 01-12-2005 09:50 PM
Reducing Photo Size Edward Eusery Computer Support 7 06-21-2004 07:08 AM



Advertisments