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