How to hide logger output in Jupyter notebook but still continue the backend logging

Viewed 698

Is there a way to hide the printed output inside Jupyter notebook (the red background section encircled in green) while still maintaining the info logging capabilities such as saving the logs into a .log file?

enter image description here

I saw threads talking about changing the LEVEL, but that is not what I am looking for since I want to keep the level at INFO to continue logging the INFO logs.

1 Answers

FYI the display in red in Jupyter is from the sys.stderr stream. What you are looking for is to perform logging that writes to file without writing to STDERR.

Following docs for the logging standard library, you can set up a logger that writes to a FileHandler, as below:

import logging
logger = logging.getLogger('my_application')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler('my_log_file.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

# Does not write to sys.stderr
logger.info("These logs do not show up on Jupyter!")
Related