Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • 6
    Option 1 does not work for non-root loggers: the method hasHandlers() also checks the handlers of the ancestor loggers, so if an ancestor logger has an handler then logger.hasHandlers() will always evaluate to True and therefore logger.handlers[0] will end up raising an IndexError. Commented Nov 24, 2020 at 1:25
  • 1
    Going off option 2, you can also just set your instantiated logger object's handlers attribute to an empty array, such as logger = logging.getLogger(__name__) and logger.handlers = [] Commented Nov 15, 2021 at 22:53
  • @deesolie yes but safer to use .clear - see @zenofpython's comment Commented Jul 6, 2024 at 14:28