Jupyter notebook does not print logs to the output cell

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.


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


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.