__del__ and logging

Discussion in 'Python' started by flupke, Apr 29, 2005.

  1. flupke

    flupke Guest

    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
     
    flupke, Apr 29, 2005
    #1
    1. Advertising

  2. flupke

    Vinay Sajip Guest

    flupke <flupke <at> nonexistingdomain.com> writes:

    >
    > 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
     
    Vinay Sajip, Apr 29, 2005
    #2
    1. Advertising

  3. flupke

    flupke Guest

    Vinay Sajip wrote:
    > flupke <flupke <at> nonexistingdomain.com> writes:
    >
    >
    >>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
    >
    >


    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
     
    flupke, May 2, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Austine
    Replies:
    2
    Views:
    358
    Steven Taschuk
    Jul 2, 2003
  2. Jane Austine
    Replies:
    1
    Views:
    474
    Erik Max Francis
    Jul 2, 2003
  3. Mike C. Fletcher
    Replies:
    2
    Views:
    305
    Mike C. Fletcher
    Jan 24, 2005
  4. Mike C. Fletcher
    Replies:
    0
    Views:
    423
    Mike C. Fletcher
    Jan 24, 2005
  5. Robert Rawlins

    RE: Logging in __del__()

    Robert Rawlins, Jul 15, 2008, in forum: Python
    Replies:
    3
    Views:
    249
    Marc 'BlackJack' Rintsch
    Jul 16, 2008
Loading...

Share This Page