Python : How to set logging level for all loggers to INFO

Viewed 10827

I am using following config

import logging


FORMAT = '%(levelname)s: %(asctime)-15s: %(filename)s: %(funcName)s: %(module)s: %(message)s'
logging.basicConfig(filename="/var/log/out.log", level=logging.INFO, format=FORMAT)
LOGGER = logging.getLogger("Customer")

And then there some libraries which I have imported. Those Libraries have debug logging statements like

 LOGGER = logging.getLogger(__name__)
 LOGGER.debug('add_timeout: added timeout %s; deadline=%s at %s',
                     timeout_id, deadline, timeout_at)

When I run my program it prints debug logs of internal libraries as well. I want to avoid debug logs at all.

1 Answers

The below code should do the trick.

loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
for logger in loggers:
    logger.setLevel(logging.INFO)

The first line returns the list of logs instantiated. The for loop just sets the level to all the logs.

P.S. credits to https://stackoverflow.com/a/53250066/4696783

Related