Search code examples
pythonjupyter-notebookpython-importpython-logging

How to prevent log messages from being printed to cell output?


Importing pygwalker (v0.1.11) changes what logging messages are displayed in the cell output. I can temporarily remove this import to prevent the messages from being logged, but I was wondering if there is an intended way to control the log messages displayed in Jupyter.

This example does not print the log message:

import logging
import numpy
import pandas

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("test")

This example (below) does print the log message:

import pygwalker
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("test")

Is there some way to programmatically determine the log messages that are printed in the cell output that can be performed regardless of the package imported?

What I've tried

  • I have tried removing the import and this resolved the issue. However, I would like to be able to import this package AND control the log messages printed to the cell output.
  • I have reported this as an issue on the GitHub repo for pygwalker.
  • This question is related to Jupyter lab: Stop the loging messages printing out to cell output but the imported package is different, and this one includes a minimally reproducible example.

Solution

  • Logging in Jupyter Notebook can be configured by adding (or removing) logging.StreamHandler from a logger. Here are a few simple solutions to the issue in the question is the following:

    Remove all logging.StreamHandler from the logger in question

    logger.handlers = list(
        filter(
            lambda handler: not isinstance(handler, logging.StreamHandler), 
            logger.handlers
        )
    )
    

    Overwrite previous calls to basicConfig

    logging.basicConfig(handlers=[logging.NullHandler()], force=True)
    

    This sets the default handler for any new loggers to be logging.NullHandler(). force=True is set because this call would be ignored if an imported package has already called basicConfig. A disadvantage with this approach is that it will become the default for all new loggers, so it lacks granular control.

    Clear all previous handlers from the logger in use

    logger.handlers.clear()
    

    One potential disadvantage with this approach is if the logger in question had other important handlers configured, this would remove them entirely.