-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated logging messages when importing module twice #11
Comments
Handout uses a separate logger instance, so you can do this: import handout
import logging
logging.getLogger('handout').setLevel(logging.ERROR) I'm also open to changing which messages are printed by default. For example, I think Do you have thoughts on this? |
Taking a second look at what you say, it does seem incorrect that the same figure is added multiple times. Have you tried just running your script as It's not entirely clear to me how Python Handout would be supposed to behave in an interactive console. It goes in a different direction than what motivated the library, i.e. to create reports in a reproducible environment. But I'd be happy to talk through how this could work and if we can extend it in a reasonable way. |
Changing the logging as suggested above did not help in IPython interactive session, each |
Maybe a path should be printed just for |
Re amount of logging messages, I reopened #2 to track this for the next release. Re executing from within Spyder, could you describe the setup a bit more, please? I assume There is no global state in Handout, so as long as you create a new |
There is no global state in Handout indeed - but there is seemingly is some state in IPython logger configuration - it accumulates messages (maybe it is a desired behaviour for a REPL). Minimal example:
Running once on new kernel in IPython (same output for
But on next runs I get multiple logger messages, one more message per run:
P.S. This is not a terrible thing, but rather a nuisance. |
Maybe I can fill a question on Stack Overflow to ask for clues. |
Thanks a lot for investigating! I'm quite surprised how the resulting handout looks like. Does it show the code lines that were executed in the REPL and in the correct order? |
I should have mentioned the html output is normal (the 'handout'), the duplication of messages appears only in the IPython console. One more line in the example:
On the first run the console output is:
On the second:
Both print() and logger messages get accumulated in the console. |
Thanks. I was actually curious how the resulting handout looks like, because there is no source file when running handout from the interactive interpreter. Besides this, would it be fair to say that this is a problem with IPython (and we can notify them about it) or is there something we can and should do on our end? |
There is the source when running with I filled an issue with IPython, maybe someone can shed some light on it. The issue is definitely on IPython side, but maybe we can do some tricky configuration of logger at init to prevent message duplication. |
The best of my understanding is that a logger object a session-level singleton, that exists in IPython session. Any time the script is run in the session again the Any time there is a new call Would need a bit of more research to find out what to do about it (in principle - randomise creation of a logger name or cut out adding more handlers to the logger). |
This reminds me a bit of duplicate logging messages I've seen in TensorFlow. What do you think about getting rid of the logger and just using |
Let me bear with this issue for a little while - it appears the problem is not even IPython, but it is specific to Spyder. I came up to running the script in IPython session in console and the messages do not duplicate:
Most Stack Overflow answers like this one deal with a situation where there is The wierd point is to detect where the handlers get added in between the calls to the script. I would like to investigate a bit, if no result, can possibly use print. Maybe wrap print in own helper:
Some people may see logging vs printing as a matter of code style, so the decision will be up to you as an author and maintainer of the project. |
God, @danijar! ))) you are actually adding a new handler in Maybe this code should be inside https://github.com/danijar/handout/blob/master/handout/__init__.py#L1-L13
What happens in that IPython resets logger handlers after As a quick fix, we can do something like below for
|
The fix in import sys
import logging
def create_logger():
logger = logging.getLogger('handout')
logger.setLevel(logging.INFO)
logger.propagate = False # Global logger should not print messages again.
if not logger.handlers:
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(handler)
# The logger can be access anywhere in code by its name
# with logging.getLogger('handout') but we pass it explcitly
# as function result
return logger later in Handout constructior:
|
Thanks for investigating! Yes, we shouldn't keep adding handlers :D The reason to create it in |
User @epogrebnyak found the bug. Fixes #11. Closes #27.
I run example.py in IPython console in Spyder and it seems that console output gets proportional to the number of times I have run the script. On the first run in new kernel I get just single message, one per call of
handout.Handout
instance method.This is what I get on the fifth run:
Maybe not a bug, but it would have felt more confortable the
handout.Handout
instance made just a single print to the screen per call.I run the script as
runfile('D:/github/handout/example.py', wdir='D:/github/handout')
The text was updated successfully, but these errors were encountered: