Reducing log size

Discussion in 'Perl Misc' started by Cosmic Cruizer, Feb 7, 2009.

  1. 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.
    Cosmic Cruizer, Feb 7, 2009
    #1
    1. Advertising

  2. Cosmic Cruizer <> 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/"
    Tad J McClellan, Feb 7, 2009
    #2
    1. Advertising

  3. 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
    Xho Jingleheimerschmidt, Feb 7, 2009
    #3
  4. Ben Morrow <> wrote in
    news::


    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
    Cosmic Cruizer, Feb 8, 2009
    #4
  5. Cosmic Cruizer

    Dr.Ruud Guest

    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
    Dr.Ruud, Feb 8, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andoni
    Replies:
    4
    Views:
    774
    Andoni
    Jul 21, 2005
  2. Dan Shelby
    Replies:
    7
    Views:
    534
    Dan Shelby
    Jan 26, 2005
  3. Martin

    Techniques for reducing code size?

    Martin, Aug 25, 2004, in forum: C Programming
    Replies:
    2
    Views:
    399
    Thomas Matthews
    Aug 25, 2004
  4. Replies:
    2
    Views:
    292
    santosh
    Nov 30, 2006
  5. Chris Thomasson
    Replies:
    11
    Views:
    524
    Jerry Coffin
    Apr 23, 2007
Loading...

Share This Page