How to disconnect registrars from other modules?

I use several modules in my project, however the modules output a lot of logs from the logger, which is annoying. Therefore, I will disable the logs:

boto_log = logging.getLogger("boto")
boto_log.setLevel(logging.CRITICAL)
es_log = logging.getLogger("elasticsearch")
es_log.setLevel(logging.CRITICAL)
urllib3_log = logging.getLogger("urllib3")
urllib3_log.setLevel(logging.CRITICAL)

Although this works, the code looks verbose. Is there a better, simpler way I can do this?

+4
source share
2 answers

Maybe you can reorganize it to cut out part of the template:

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

You can disable existing logs with logging.config.dictConfigor logging.config.fileConfig.

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

You can also iterate over existing logs and disable them manually.

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

Source: https://habr.com/ru/post/1568082/


All Articles