Skip to content

The python logging module no longer seems to work properly inside a notebook #1397

@tudorprodan

Description

@tudorprodan

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions