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 tha
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.