Loggers and reloads

Discussion in 'Python' started by Francois Bouffard, May 26, 2004.

  1. (Disclaimer: I'm new to both Python and OOP in general, and I may have
    skipped a few lines in the docs)

    I'm using the logging module inside one of my module. I instanciate a
    logger which uses a StreamHandler to output to sys.stdout. It works
    really well; however, I mainly use my module in the Python interpreter
    in an interactive way, and I often have to reload said module.

    The problem is that each time I reload my module, a new logger object
    seems to be created, and the old one is not deleted. Both the new and
    the old object are still working, so that each log message is
    repeated; in general, if I did N reloads of my module, each message is
    repeated N+1 times.

    The del statement seems to only delete references, so that it can't be
    used to get rid of the old logger objects. Maybe the
    logging.shutdown() function is supposed to be of some help here, but I
    don't see how to use it. Any hint as to what I'm doing wrong or what I
    should do is welcome.

    Here's some sample module code:

    module logtest.py
    ---------------
    import sys, logging

    LOGLEVEL = logging.INFO
    loghandler = logging.StreamHandler(sys.stdout)
    logformat = logging.Formatter('%(name)s :: %(message)s')
    loghandler.setFormatter(logformat)
    logger = logging.getLogger('LoggerExample')
    logger.addHandler(loghandler)
    logger.setLevel(LOGLEVEL)

    def info_message():
    logger.info('This is an info-level message')
    ---------------

    >>> import logtest
    >>> logtest.info_message()

    LoggerExample :: This is an info-level message
    >>> reload(logtest)

    <module 'logtest' from 'logtest.pyc'>
    >>> logtest.info_message()

    LoggerExample :: This is an info-level message
    LoggerExample :: This is an info-level message
    Francois Bouffard, May 26, 2004
    #1
    1. Advertising

  2. Francois Bouffard wrote:

    > The problem is that each time I reload my module, a new logger object
    > seems to be created, and the old one is not deleted. Both the new and
    > the old object are still working, so that each log message is
    > repeated; in general, if I did N reloads of my module, each message is
    > repeated N+1 times.


    The problem would disappear if you wrote a small testscript that uses your
    module and execeted that from scratch, instead of reloading the module -
    which might also create other state related problems.

    But if you insist on using your module, you could place the logger
    initialization into another module or encapsulate it in a function that you
    call manually - but only once. As the logging systems state is kept in the
    not-reloaded logging module, that should rid you of your problem. I bet it
    would also be possible to check if for a given logger already a handler is
    registered, and refuse to add another one.

    --
    Regards,

    Diez B. Roggisch
    Diez B. Roggisch, May 26, 2004
    #2
    1. Advertising

  3. > I bet it
    > would also be possible to check if for a given logger already a handler is
    > registered, and refuse to add another one.


    You're completely right, thank you. There's always only one logger
    object in my code, however, each time the module is reloaded, a new
    handler is added. Therefore, something like:

    for h in logger.handlers:
    logger.removeHandler(h)

    indeed does the trick if I put it before the logger.addHandler() call.

    Thanks again.
    =?ISO-8859-1?Q?Fran=E7ois_Bouffard?=, May 27, 2004
    #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. Simo-Pekka Sadeluoto

    loggers with different handler properties

    Simo-Pekka Sadeluoto, Jul 8, 2003, in forum: Java
    Replies:
    0
    Views:
    527
    Simo-Pekka Sadeluoto
    Jul 8, 2003
  2. nico
    Replies:
    3
    Views:
    438
    Arne Vajhøj
    Jan 19, 2008
  3. Rhino
    Replies:
    8
    Views:
    501
    markspace
    Apr 30, 2010
  4. Tim

    os.system and loggers

    Tim, Jan 7, 2011, in forum: Python
    Replies:
    4
    Views:
    264
  5. Tomer
    Replies:
    14
    Views:
    276
    Arne Vajhøj
    Jan 5, 2013
Loading...

Share This Page