20

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.

hard coder
  • 5,449
  • 6
  • 36
  • 61
  • 1
    Unless the library configures its loggers itself (which it should not do, leaving this up to the application) the global logging level set in `logging.basicConfig()` should be respected so there should be no need to set logging levels of library's loggers. – Piotr Dobrogost Jun 09 '22 at 09:41

1 Answers1

40

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

alcaprar
  • 739
  • 8
  • 18