Logging in __del__()

Robert Rawlins robert.rawlins at thinkbluemedia.co.uk
Tue Jul 15 07:32:06 EDT 2008


Guys,

 

I'm trying to help trace when instances of particular classes are being
destroyed by the garbage collector and thought the cleanest way would be to
implement a logging call in __del__() on the class. However, I'm having an
issue.

 

I inject a logger instance into my class upon construction and set it to
self.logger, I then use this within the class to log actions going on
within, works fine.

 

Now, when I make the call in the __del__() method like so:

 

    def __del__(self):

        # Log the classes destruction.

        self.logger.debug("Class Instance Destroyed")

 

I then get the following exception thrown when running my code:

 

Traceback (most recent call last):

  File "/usr/lib/python2.5/logging/handlers.py", line 73, in emit

    if self.shouldRollover(record):

  File "/usr/lib/python2.5/logging/handlers.py", line 147, in shouldRollover

    self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature

ValueError: I/O operation on closed file

 

Does anyone have any ideas as to what I'm doing wrong here? Is this a known
issue which has a neat little work around?

 

Cheers,

 

Robert

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080715/78f8d6bc/attachment.html>


More information about the Python-list mailing list