__del__ and logging

F

flupke

Hi,

i have a class and a class attribute log which is a logger object. In
the __del__() function i want to log a message but it fails even if i
use self.__class__.log.

The error i get is this:
Traceback (most recent call last):
File "C:\Python24\lib\logging\__init__.py", line 712, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file

So is there no way to use the logger object in a __del__
I wanted to use the message to clearly indicate in the logger file that
the instance had closed ok.

Regards,
Benedict
 
V

Vinay Sajip

flupke said:
Hi,

i have a class and a class attribute log which is a logger object. In
the __del__() function i want to log a message but it fails even if i
use self.__class__.log.

The error i get is this:
Traceback (most recent call last):
File "C:\Python24\lib\logging\__init__.py", line 712, in emit
self.stream.write(fs % msg)
ValueError: I/O operation on closed file

So is there no way to use the logger object in a __del__
I wanted to use the message to clearly indicate in the logger file that
the instance had closed ok.

It all depends. If your __del__ is being called via atexit() for application
cleanup, for example, logging may not be available to you because it has been
cleaned up beforehand.

The logging module registers an atexit() handler to flush and close handlers
before script termination.

Vinay Sajip
 
F

flupke

Vinay said:
It all depends. If your __del__ is being called via atexit() for application
cleanup, for example, logging may not be available to you because it has been
cleaned up beforehand.

The logging module registers an atexit() handler to flush and close handlers
before script termination.

Vinay Sajip

Thanks i was able to solve it using a function registered in both the
classes i used in that project.
Why would one use __del__ instead of atexit then especially if the calls
you make in __del__ are so unrelieable?

Regards,
Benedict
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top