Re: Logging to a file and closing it again properly (logging module)

Discussion in 'Python' started by Christoph Haas, Jun 14, 2006.

  1. On Wed, Jun 14, 2006 at 12:29:22PM +0530, Amit Khemka wrote:
    > On 6/13/06, Christoph Haas <> wrote:
    > >I have an application that is running in an endless loop processing an
    > >incoming queue. Every run is supposed to write a log file about the run
    > >and then close it again. While the 'logging' module is generally working
    > >well (even though the documentation makes me miss some proper examples
    > >how everything works together) I can't seem to close the log file again
    > >to open a new one.
    > >
    > >This is basically what I'm doing:
    > >
    > >log = logging.getLogger("myapplication")
    > >log.addHandler(logging.FileHandler("/tmp/testfile"))
    > >log.setLevel(logging.INFO)
    > >log.info("foo")
    > >
    > >Now I'm missing a way to tell this handler to go away. Through 'ipython'
    > >I found out there is a log.handlers array that contains all the
    > >handlers. Perhaps I could delete all of them but I'm sure there is a
    > >more proper way to close files again.
    > >
    > >Googling found me:
    > >
    > > .>>> logging._handlers.clear()
    > > .>>> logging.root.handlers = []
    > > .>>> for l in logging.Logger.manager.loggerDict.values():
    > > .>>> l.handlers = []

    >
    > You can "close" the logger by just removing the handler from the logging
    > object
    >
    > # first creater a logger and file handler
    > fooLogger = logging.getLogger('FOO')
    > fooLogger.setLevel(logging.INFO)
    > fl = logging.FileHandler('foo.txt)
    > fl.setLevel(logging.INFO)
    > fooLogger.addHandler(fl)
    >
    > # remove the handler once you are done
    > fooLogger.removeHandler(fl)


    Thanks. That works great. I was looking for a way to close the log file
    using logging.basicConfig but it appears that it's too much magic.
    logging.getLogger() isn't really much more work but gives better
    control.

    Kindly
    Christoph
    Christoph Haas, Jun 14, 2006
    #1
    1. Advertising

  2. Christoph Haas

    Vinay Sajip Guest

    Christoph Haas wrote:

    > Thanks. That works great. I was looking for a way to close the log file
    > using logging.basicConfig but it appears that it's too much magic.
    > logging.getLogger() isn't really much more work but gives better
    > control.


    You may also need to close the handler - removeHandler just removes the
    handler from the Logger's internal list.

    If there are specific areas in the documentation which you feel are
    lacking clarity, please post more specific information. Documentation
    patches are especially welcome ;-)

    Regards,

    Vinay Sajip
    Vinay Sajip, Jun 14, 2006
    #2
    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. =?Utf-8?B?Q2hyeXNhbg==?=

    Why popup window will be re-opened again after closing it?

    =?Utf-8?B?Q2hyeXNhbg==?=, Sep 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,322
    =?Utf-8?B?Q2hyeXNhbg==?=
    Oct 3, 2005
  2. amit
    Replies:
    0
    Views:
    388
  3. Christoph Haas
    Replies:
    0
    Views:
    440
    Christoph Haas
    Jun 12, 2006
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    649
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    226
    thomas
    Oct 23, 2003
Loading...

Share This Page