notebook: The python logging module no longer seems to work properly inside a notebook
When running the example from the python logging documentation page in a jupyter notebook, the messages go to the notebook console as opposed to the notebook’s webpage output via the capture mechanism. Just create an empty notebook, run the example below, you’ll see what I mean.
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
The logging module by default creates a StreamHandler
object which will if not stream: stream = sys.stderr
. A lot of modules use the logging module as in the example.
The problem is that the notebook actually inits the logging module before the output capture mechanism is set, so the default created StreamHandler
actually has a reference to the original sys.stdout
, before the notebook replaces it with it’s own ipykernel.iostream.OutStream
.
Shouldn’t the notebook reset the logging module?
I’ve been looking into this because after an upgrade I noticed missing output from some stuff I was running often.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (7 by maintainers)
@tudorprodan I tried reload(logging) which works for me, see it here http://stackoverflow.com/a/21475297 hope it helps