(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
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
Comment