logging error with RotatingFileHandler

flupke flupke at nonexistingdomain.com
Fri Jun 30 09:11:10 EDT 2006


flupke schreef:
<snip>
> Hi Vinay,
> 
> thanks for the info.
> 
> I tried to change the handlers.py file.
> First, i changed some code in doRollover.
> 
> if os.path.exists(dfn):
>     os.remove(dfn)
> try:
>     #os.rename(self.baseFilename, dfn)
> 
> -> The rename fails for some reason.
> 
> I tried with the move function of shutil
>     shutil.move(self.baseFilename, dfn)
> 
> This partially works: the file is now rotated but i still get an error:
> The error that i get is this:
> Error  [Errno 13] Permission denied: 'E:\\python\\proj1\\src\\proj1.log'
> Traceback (most recent call last):
>   File "C:\Python24\lib\logging\handlers.py", line 72, in emit
>     self.doRollover()
>   File "C:\Python24\lib\logging\handlers.py", line 141, in doRollover
>     self.handleError(record)
> NameError: global name 'record' is not defined
> 
> The last part of the error shows that the part where the exception is
> caugth is also not working correctly as the "record" isn't know.
> 
> When i close my program, i get this error as well:
> 
> Error in atexit._run_exitfuncs:
> Traceback (most recent call last):
>   File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
>     func(*targs, **kargs)
>   File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
>     h.close()
>   File "C:\Python24\lib\logging\__init__.py", line 772, in close
>     StreamHandler.close(self)
>   File "C:\Python24\lib\logging\__init__.py", line 674, in close
>     del _handlers[self]
> KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>
> Error in sys.exitfunc:
> Traceback (most recent call last):
>   File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
>     func(*targs, **kargs)
>   File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
>     h.close()
>   File "C:\Python24\lib\logging\__init__.py", line 772, in close
>     StreamHandler.close(self)
>   File "C:\Python24\lib\logging\__init__.py", line 674, in close
>     del _handlers[self]
> KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>
> 
> Regards
> Benedict

I was able to solve the errors (i think):

1. in the function doRollover of the class RotatingFileHandler

if os.path.exists(dfn):
    os.remove(dfn)
try:
    os.rename(self.baseFilename, dfn)

I added this instead of the rename function:
import shutil
shutil.copy(self.baseFilename, dfn)

If this code works ok, then the "import shutil" should be placed at the
top of the file for performance reasons.
The logic of the copy is that a few statements later, the self.stream
var is set to a new file again so it doesn't need to be deleted. At
least it seems to work here. I will need to test further.
Weird thing is, the stream is closed and yet you get this error when you
use rename:
[Errno 13] Permission denied

2. in the same function, when an error occurred, the record var wasn't
found.
This can be solved by changing the code of emit in BaseRotatingHandler.
Change this:
if self.shouldRollover(record):
    self.doRollover()
by:
if self.shouldRollover(record):
    self.doRollover(record)

3. The last error (error when closing the program) can be solved by
editing the __init__.py, close(self) function.
Change this:
_acquireLock()
try:    #unlikely to raise an exception, but you never know...
    del _handlers[self]
    _handlerList.remove(self)
finally:
    _releaseLock()

by:
_acquireLock()
try:    #unlikely to raise an exception, but you never know...
    #del _handlers[self]
    if ( _handlers.has_key(self) ): del _handlers[self]
    #if ( self in _handlerList ): _handlerList.remove(self)
    _handlerList.remove(self)
finally:
    _releaseLock()

It might even be better to change the _handlerList.remove(self)
statement by this statement:
    if ( self in _handlerList ): _handlerList.remove(self)

Regards,
Benedict Verheyen



More information about the Python-list mailing list