in reply to Log rotating filter.

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