I am currently using Jupyter
notebook and I would like to force it to print out Python
logs to the output cell.
I am using old notebook that used to work this way, probably in older version of Jupyter
notebook.
I have logging set as:
import logging
logging.basicConfig(format='%(levelname)s : %(message)s', level=logging.INFO)
logging.root.level = 20
But when I then call:
logging.info("hello world")
It does not print anything in the output cell. It just prints out the stuff in the console in which I started the Jupyter
notebook.
I am using python 2.7.10
and installed packages in my virtual environment are:
appnope==0.1.0
backports-abc==0.4
backports.ssl-match-hostname==3.5.0.1
certifi==2016.2.28
decorator==4.0.9
functools32==3.2.3.post2
gnureadline==6.3.3
ipykernel==4.3.1
ipython==4.1.2
ipython-genutils==0.1.0
ipywidgets==4.1.1
Jinja2==2.8
jsonschema==2.5.1
jupyter==1.0.0
jupyter-client==4.2.2
jupyter-console==4.1.1
jupyter-core==4.1.0
MarkupSafe==0.23
mistune==0.7.2
nbconvert==4.1.0
nbformat==4.0.1
notebook==4.1.0
path.py==8.1.2
pexpect==4.0.1
pickleshare==0.6
ptyprocess==0.5.1
Pygments==2.1.3
pyzmq==15.2.0
qtconsole==4.2.0
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.10.0
terminado==0.6
tornado==4.3
traitlets==4.1.0
wheel==0.24.0
Was logging printout in the cell changed? Is there some way how I can force Jupyter
to write out logging to the output cell?
Hopefully the jupyter folks will fix this. However, I found a workaround you can use for now. It seems that maybe the new versions of jupyter notebook do not show stderr in the notebook, but send stderr to the terminal instead. But, they still print stdout. You can set the handler of the root logger to stdout:
import logging
import sys
# Get root logger (all other loggers will be derived from this logger's
# properties)
logger = logging.getLogger()
logger.warning("I will output to terminal") # No output in notebook, goes to terminal
# assuming only a single handler has been setup (seems
# to be default in notebook), set that handler to go to stdout.
logger.handlers[0].stream = sys.stdout
logger.warning("FOO") # Prints: WARNING:root:FOO
# Other loggers derive from the root logger, so you can also do:
logger2 = logging.getLogger("logger2")
logger2.warning("BAR") # Prints: WARNING:logger2:BAR
If you put this at the top of your notebook, this change should propagate to any loggers initialized in modules you import as well, since generally loggers will inherit the setup of the root logger.
None of the workarounds suggested here worked for me for me, but the below did, maybe it will help somebody else.
Using python 3.4.3, jupyter-client==4.1.1, jupyter-core==4.0.6
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.info("hello")
INFO:main:hello
I have found one my old python 2.7.10
virtualenv where jupyter
works as desired.
appnope==0.1.0
backports.ssl-match-hostname==3.4.0.2
boto==2.38.0
bz2file==0.98
certifi==2015.9.6.2
decorator==4.0.4
functools32==3.2.3.post2
gensim==0.12.2
gnureadline==6.3.3
httpretty==0.8.6
ipykernel==4.1.1
ipython==4.0.0
ipython-genutils==0.1.0
ipywidgets==4.1.1
Jinja2==2.8
jsonschema==2.5.1
jupyter==1.0.0
jupyter-client==4.1.1
jupyter-console==4.0.3
jupyter-core==4.0.6
MarkupSafe==0.23
mistune==0.7.1
nbconvert==4.1.0
nbformat==4.0.1
notebook==4.0.6
numpy==1.10.1
path.py==8.1.2
Pattern==2.6
pexpect==4.0.1
pickleshare==0.5
ptyprocess==0.5
Pygments==2.0.2
pyzmq==14.7.0
qtconsole==4.1.0
requests==2.8.1
scipy==0.16.0
simplegeneric==0.8.1
six==1.10.0
smart-open==1.3.0
terminado==0.5
tornado==4.2.1
traitlets==4.0.0
wheel==0.24.0
Some packages are not related with the jupyter
problem here, problem will probably be in versions of some relevant packages. I'll try further investigate exactly where.
EDIT:
At the end I have found that the problem is in the package: ipykernel==4.3.1
or since ipykernel==4.3.0
everything works fine with ipykernel==4.1.1
or ipykernel==4.2.2
.
Issue was reported at github to the jupyter and ipykernel: https://github.com/ipython/ipykernel/issues/111
The earlier answers seem no longer to work. The most complete one no longer works because there are no default handlers, so modifying the zeroeth doesn't work. Also, messing with the root logger seems potentially fraught when running in a notebook.
In order to get the "foo"
logger to put its output in the cell you can do the following:
logger = logging.getLogger("foo")
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
So add the handler yourself, and direct its output.