In one of the scripts logging is set up like so:
logging configuration - two handlers
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('logfile')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
Pretty usual logger, right? Sure, yet it causes Spyder's IPython console to behave in a weird way.
Each subsequent script launch (Spyder's 'Run' through internal IPython console) increments the number of times each one of the log messages is being logged, ie the second time the script is launched the message appears twice, third time three times and so on.
2018-12-06 17:03:44,888 - root - DEBUG - Start.
2018-12-06 17:03:44,891 - root - DEBUG - Exit.
2018-12-06 17:03:45,982 - root - DEBUG - Start.
2018-12-06 17:03:45,982 - root - DEBUG - Start.
2018-12-06 17:03:45,984 - root - DEBUG - Exit.
2018-12-06 17:03:45,984 - root - DEBUG - Exit.
2018-12-06 17:03:49,739 - root - DEBUG - Start.
2018-12-06 17:03:49,739 - root - DEBUG - Start.
2018-12-06 17:03:49,739 - root - DEBUG - Start.
2018-12-06 17:03:49,742 - root - DEBUG - Exit.
2018-12-06 17:03:49,742 - root - DEBUG - Exit.
2018-12-06 17:03:49,742 - root - DEBUG - Exit.
Restarting the IPython's kernel or spanning a new console instance resets this behaviour. 'Resetting variables' seems to have no effect on this, should it?
I think new instances of 'fileHandlers' are added to the 'logger' object on each subsequent launch. Note exact same timestamp on each of the duplicate log entries.
Any ideas how to avoid / fix this?
I think a new instance of 'fileHandler' is added to the 'logger' object on each subsequent launch
Yes, I think that's right. To avoid this, you can detect if fh
and ch
are already defined, so you don't redefine them and add them to logging
on each run.
For that you could add a code like this:
from IPython import get_ipython
if not 'fh' in get_ipython().user_ns:
fh = logging.FileHandler('logfile')
fh.setLevel(logging.DEBUG)
...
and the same for ch
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.