Python default logger disabled

If you need to trace what code might set handler.disabled to True (it is 0, so false, by default), you can replace the attribute with a property:

import logging
import sys

@property
def disabled(self):
    try:
        return self._disabled
    except AttributeError:
        return False

@disabled.setter
def disabled(self, disabled):
    if disabled:
        frame = sys._getframe(1)
        print(
            f"{frame.f_code.co_filename}:{frame.f_lineno} "
            f"disabled the {self.name} logger"
        )
    self._disabled = disabled

logging.Logger.disabled = disabled

Demo from the interactive interpreter:

>>> import logging
>>> logging.getLogger('foo.bar').disabled = True
<stdin>:1 disabled the foo.bar logger

If you want to see the full stack, add from traceback import print_stack, and inside the if disabled: block, print_stack(frame).


Often found this problem when configuration schema is used, by default disable_existing_loggers is True so all loggers that not included in that schema will be disabled.

BTW Martin Pieters' answer is supreme and works in any situation when you've stuck.

Tags:

Python

Logging