11

The logging docs say that calling the logging.disable(lvl) method can "temporarily throttle logging output down across the whole application," but I'm having trouble finding the "temporarily." Take, for example, the following script:

import logging
logging.disable(logging.CRITICAL)
logging.warning("test")
# Something here
logging.warning("test")

So far, I haven't been able to find the Something here that will re-enable the logging system as a whole and allow the second warning to get through. Is there a reverse to disable()?

2 Answers 2

18
logging.disable(logging.NOTSET)    
Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic, and - in retrospect - pretty obvious :) Thanks!
Any way to contact python documentation team? Something is obviously missing there.
2

Based on the answer by @unutbu, I created a context manager:

import logging
log = logging.getLogger(__name__)

class SuppressLogging:
    """
    Context handler class that suppresses logging for some controlled code.
    """

    def __init__(self, loglevel):
        logging.disable(loglevel)
        return

    def __enter__(self):
        return 

    def __exit__(self, exctype, excval, exctraceback):
        logging.disable(logging.NOTSET)
        return False

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    log.info("log this")
    with SuppressLogging(logging.WARNING):
        log.info("don't log this")
        log.warning("don't log this warning")
        log.error("log this error while up to WARNING suppressed")
    log.info("log this again")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.