[issue8165] logging.handlers.RotatingFileHandler fails when delay parameter is set to True

Eli Courtwright report at bugs.python.org
Wed Mar 17 20:58:12 CET 2010


New submission from Eli Courtwright <eli.courtwright at gmail.com>:

Here's a summary of the issue (also presented at http://stackoverflow.com/questions/2465073)

When I run the following code

{{{
import logging
from logging.handlers import RotatingFileHandler

rfh = RotatingFileHandler("testing.log", delay=True)
logging.getLogger().addHandler(rfh)
logging.warning("Boo!")
}}}

then the last line throws "AttributeError: RotatingFileHandler instance has no attribute 'level'".  So I add the line

{{{
rfh.setLevel(logging.DEBUG)
}}}

before the call to addHandler, and then the last line throws "AttributeError: RotatingFileHandler instance has no attribute 'filters'". So if I manually set filters to be an empty list, then it complains about not having the attribute "lock", etc.

When I remove the delay=True, the problem completely goes away.

So one of the parent __init__ methods somewhere up the class hierarchy isn't getting called when delay is set. Examining the source code to the file logging/__init__.py, I see the following code in the FileHandler.__init__ method:

{{{
if delay:
    self.stream = None
else:
    stream = self._open()
    StreamHandler.__init__(self, stream)
}}}

It looks like the FileHandler.emit method checks for un-opened streams and finishes initialization when logging is performed:

{{{
if self.stream is None:
    stream = self._open()
    StreamHandler.__init__(self, stream)
StreamHandler.emit(self, record)
}}}

So the problem is that in the BaseRotatingHandler.emit method, the shouldRollover and doRollover methods are called before emit-ing the record. This causes methods to be called which themselves assumed that the __init__ process has completed.

Unfortunately, I don't have time right now to submit a patch, though I might be able to find the time within the next few months if no one else gets to it first.  Hopefully this report is detailed enough to easily convey the problem.

----------
components: Library (Lib)
messages: 101242
nosy: Eli.Courtwright
severity: normal
status: open
title: logging.handlers.RotatingFileHandler fails when delay parameter is set to True
type: crash
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8165>
_______________________________________


More information about the Python-bugs-list mailing list