[Tutor] logging

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Feb 23 13:56:42 EST 2004



On Mon, 23 Feb 2004, helena bhaska wrote:

> I was wondering if there is a way to limit the file size when I use
> python logging facility to write messages to a file? I would like to
> configure it so that the file does not exceed n number of lines. Thanks!

Hi Helena,


Interesting question!  Yes, it's possible to do this.  The handler
"RotatingFileHandler", for example, implements similar behavior:

    http://www.python.org/doc/lib/node285.html


It might be possible to update RotatingFileHandler to count the number of
lines rather than the number of bytes, but I'm not sure if it's worth the
effort to do it exactly.  *grin*


The quick way to do this is to just set the handler of your logger to a
RotatingFileHandler instance, and set the byte limit to 'n' times some
constant, like 80 bytes.  (Since the number of lines should be roughly
proportional to the number of bytes, we'll only be off by about a constant
factor.)


Talk to you later!




More information about the Tutor mailing list