Cropping log files

Kamus of Kadizhar yan at NsOeSiPnAeMr.com
Tue Dec 16 06:08:22 EST 2003


Yet another possible newbie question.

I'm tyring to figure out how to best crop a log file.  I have a file 
that grows infinitely in length.  I want to keep only the last n entries 
in the file.

I've been looking for some sort of ascii-database or log file management 
python module that lets me say: how many records in the file? and then 
say: delete the first nr - n records.

No joy.

I don't want to suck the whole file into memory if I can help it, and I 
can't help thinking that doing a
for line in file(logfile)
    nr += 1

to count the number of lines, then reading the file again, discarding 
the first nr - n records, writing the rest to a temp file, and then 
renaming the files is the most efficient way to go.

Not only that, but what happens if the logfile is written to while I'm 
doing all of this - I may lose log file entries....

I found FLAD, even that seems to be overkill for what I need. So, any 
modules out there for log file management?

The logging module lets me log events, but there aren't any tools for 
managing the log files created in the way I need....  The 
RotatingFileHandler rotates logs in the sense of logrotate, but what I 
need is to keep only the last n records in one file.

-Kamus

-- 
     o__      |  If you're old, eat right and ride a decent bike.
     ,>/'_    |                                       Q.
    (_)\(_)   |                                       Usenet posting`





More information about the Python-list mailing list