[Tutor] How to limit logs
Wayne
srilyk at gmail.com
Fri Jul 17 16:12:57 CEST 2009
On Fri, Jul 17, 2009 at 2:12 AM, barış ata <brsata at gmail.com> wrote:
> hi,
> i'm writing a log client to recieve logs from socket connection and display
> on a textbox
> now i have a few question
> i used Pmw.ScrolledText but i don't now its buffer size. how can i limit
> it?
> and i want to create a log file with logging module but i want to limit log
> size
> for example if user sets log file limit to 1000 lines, i have to save last
> 1000 line on the fly and refresh it until program closed. Any idea ?
I don't know if this is the "best" solution, but while the program is open
if you simply truncate the file each time and just keep a list of no larger
than 1000 lines and write each of those, that would work.
Another option is to keep those 1000 lines and just seek to the beginning
and rewrite the file each time. This could get pretty expensive as far as
disk access goes, though. I don't think there's really a way in python to
implement the disk access part, but a circular array (or file write) would
work perfect in this situation.
But with python, I'd probably just open the file in read/write mode
(assuming you want to preserve the log from the previous run), seek to the
beginning and write whatever is in the lines. Each time I add a line I'd
check len(lines) > 1000: del(lines[0]), then write the lines and seek back
to the front of the file.
I don't know if there's a more efficient way to do it in python, but that's
what I'd do.
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090717/ff075981/attachment.htm>
More information about the Tutor
mailing list