4

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kenneth Leung
  • 300
  • 3
  • 8
  • Am also open to answers that use the default `logging` library in Python, instead of loguru! – Kenneth Leung Jan 14 '22 at 10:33
  • Welcome to SO! Please read "[Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/128421)" and "[Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812)" – the Tin Man Jan 23 '22 at 22:00
  • Is something supposed to be circled in green? – eric May 05 '23 at 12:22

1 Answers1

0

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!")
tnwei
  • 860
  • 7
  • 15