Python logging disable














































Python logging disable



Python logging disable


Description: It provides an overriding logging level for all loggers which takes precedence over the logger's own level. When the need arises to temporarily throttle logging output down across the whole application, this function can be useful. Its effect is to disable all logging calls of severity level and below, so that if you call it with a value of INFO, then all INFO and DEBUG events would be discarded, whereas those of severity WARNING and above would be processed according to the logger's effective level. If logging.disable(logging.NOTSET) is called, it effectively removes this overriding level, so that logging output again depends on the effective levels of individual loggers.


Note that if you have defined any custom logging level higher than CRITICAL (this is not recommended), you won't be able to rely on the default value for the level parameter, but will have to explicitly supply a suitable value.


Code:
import logging
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSET

logging.basicConfig(level=logging.DEBUG)

def logging_levels():
    logging.debug("This is debug")
    logging.info("This is info")
    logging.warning("This is warning")
    logging.error("This is error")
    logging.critical("This is critical")

def logging_levels_above_than(LevelValue=NOTSET):
    logging.disable(level=LevelValue)

    logging.debug("This is debug")
    logging.info("This is info")
    logging.warning("This is warning")
    logging.error("This is error")
    logging.critical("This is critical")

if __name__ == "__main__":
    logging_levels()
    print("\n")
    logging_levels_above_than(INFO)
Output:(Console)


Explanation: In this code, we are using two function. In one function we use all the logging levels. We set Log level value to DEBUG in basicConfig function. When function logging_levels executed, all the level of logging message will printed on console. In another function we used disable function to escape or we can say disable up to that logging level messages and also we use levelValue keyword argument and set its value to NOTSET. If we don't pass any value to this function then it will take NOTSET by default. In this function we pass INFO value as an argument, disable function will ignore that level and below that level of messages. We passed INFO as an argument so we can't see INFO, DEBUG level of messages. We can see only above that level of messages like WARNING, ERROR, CRITICAL.


Code:
import logging
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSET

logging.basicConfig(level=logging.DEBUG)

def logging_levels():
    logging.debug("This is debug")
    logging.info("This is info")
    logging.warning("This is warning")
    logging.error("This is error")
    logging.critical("This is critical")

def logging_levels_above_than(LevelValue=NOTSET):
    logging.disable(level=LevelValue)

    logging.debug("This is debug")
    logging.info("This is info")
    logging.warning("This is warning")
    logging.error("This is error")
    logging.critical("This is critical")

if __name__ == "__main__":
    logging_levels()
    print("\n")
    logging_levels_above_than(WARNING)
Output:(Console)


ExplanationWe take one more example to clarify disable function. In this code we passed WARNING as a logging level then disable function will ignore all logging messages which is having WARNING, INFO and DEBUG level. It will show only ERROR and CRITICAL logging messages.



Each and every software has its own need to log. Some softwares need error, critical messages with warning messages too. Some softwares do not need warning messages, they need error and critical messages only. On the basis of Software Structure we can implement log messages and ignore another messages using disable function.




Comments