Created
December 8, 2013 03:48
-
-
Save simon-weber/7853144 to your computer and use it in GitHub Desktop.
A context manager to temporarily disable all logging in Python that supports previous calls to logging.disable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import logging | |
@contextmanager | |
def all_logging_disabled(highest_level=logging.CRITICAL): | |
""" | |
A context manager that will prevent any logging messages | |
triggered during the body from being processed. | |
:param highest_level: the maximum logging level in use. | |
This would only need to be changed if a custom level greater than CRITICAL | |
is defined. | |
""" | |
# two kind-of hacks here: | |
# * can't get the highest logging level in effect => delegate to the user | |
# * can't get the current module-level override => use an undocumented | |
# (but non-private!) interface | |
previous_level = logging.root.manager.disable | |
logging.disable(highest_level) | |
try: | |
yield | |
finally: | |
logging.disable(previous_level) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice 💯