How to disable loggers from other modules?

May be you can refactor it in order to cut some of the boilerplate:

for _ in ("boto", "elasticsearch", "urllib3"):
    logging.getLogger(_).setLevel(logging.CRITICAL)

You should know exactly name of logger which should be desabled. "urllib3.connectionpool" != "urllib3".

logging.getLogger("boto").disabled = True
logging.getLogger("elasticsearch").disabled = True
logging.getLogger("urllib3").disabled = True
logging.getLogger('urllib3.connectionpool').disabled = True

You can get a list of all loggers (excluding the root logger) from logging.root.manager.loggerDict.

for _ in logging.root.manager.loggerDict:
    logging.getLogger(_).setLevel(logging.CRITICAL)
    # logging.getLogger(_).disabled = True  # or use this instead of CRITICAL if you'd rather completely disable it

This allows you the flexibility to put in your own filter etc if you'd rather actually keep some loggers.


You can disable existing loggers with either logging.config.dictConfig or logging.config.fileConfig.

import logging.config
logging.config.dictConfig({
    'version': 1,
    # Other configs ...
    'disable_existing_loggers': True
})

You can also loop over existing loggers and disable manually.

for name, logger in logging.root.manager.loggerDict.iteritems():
    logger.disabled=True

Tags:

Python

Logging