I have done something very similar to this before, except that I had some different requirements that made things a bit more complicated. We were most concerned with the logs filling up our disk, so instead of rotating the logs, we wanted a "rolling" log that would keep the last 100MB of logs or so. Except that we also wanted to keep some of the log messages (based on severity) permanently.
Still, from that experience, I can tell you that your pseudocode should work just fine. You don't necessarily have to rename the old log, though -- you could just reopen LOG with a new filename. Number them sequentially, or use some part of the time and date, or whatever. So something like (untested) this ought to work, as long as you don't need to restart your app using the same set of logs:
my $log_number = 0;
my $size = $MAXSIZE+1;
while (<>) {
if ($size > $MAXSIZE) {
# This will automatically close the old log file
open(LOG, ">log-" . ++$log_number)
or die "open: $!";
$size = 0;
}
print LOG $_;
$size += length($_);
}
Although if you wanted to be able to seek directly to some offset within the overall log file, you might be better off using a global $size counter and naming the log files by the offset of their starting byte, or perhaps an initial line number (you'd just need to keep a $next_start = $size + $MAXSIZE around).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.