Loggers and reloads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francois Bouffard

    Loggers and reloads

    (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.shutdow n() 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.StreamH andler(sys.stdo ut)
    logformat = logging.Formatt er('%(name)s :: %(message)s')
    loghandler.setF ormatter(logfor mat)
    logger = logging.getLogg er('LoggerExamp le')
    logger.addHandl er(loghandler)
    logger.setLevel (LOGLEVEL)

    def info_message():
    logger.info('Th is is an info-level message')
    ---------------
    [color=blue][color=green][color=darkred]
    >>> import logtest
    >>> logtest.info_me ssage()[/color][/color][/color]
    LoggerExample :: This is an info-level message[color=blue][color=green][color=darkred]
    >>> reload(logtest)[/color][/color][/color]
    <module 'logtest' from 'logtest.pyc'>[color=blue][color=green][color=darkred]
    >>> logtest.info_me ssage()[/color][/color][/color]
    LoggerExample :: This is an info-level message
    LoggerExample :: This is an info-level message
  • Diez B. Roggisch

    #2
    Re: Loggers and reloads

    Francois Bouffard wrote:
    [color=blue]
    > 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.[/color]

    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

    Comment

    • François Bouffard

      #3
      Re: Loggers and reloads

      > I bet it[color=blue]
      > would also be possible to check if for a given logger already a handler is
      > registered, and refuse to add another one.[/color]

      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.removeHa ndler(h)

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

      Thanks again.

      Comment

      Working...