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 loggingfrom logging import DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSETlogging.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 loggingfrom logging import DEBUG, INFO, WARNING, ERROR, CRITICAL, NOTSETlogging.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)
Explanation: We 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.
Name | Views | Likes |
---|---|---|
Python logging log | 356 | 0 |
Python logging getChild | 1030 | 0 |
Python logging config | 378 | 0 |
Python logging getLogger | 403 | 0 |
Python logging getLevelName | 539 | 0 |
Python Restaurant Management | 559 | 1 |
Python logging info | 514 | 0 |
Python logging makeLogRecord | 556 | 0 |
Python logging getLoggerClass | 466 | 0 |
Python logging getEffectiveLevel | 364 | 0 |
Python logging setLoggerClass | 772 | 0 |
Python logging Logger | 311 | 0 |
Python logging LogRecord | 853 | 0 |
Python logging Introduction | 301 | 0 |
Python logging Filter | 965 | 0 |
Python logging Two Different Files with logging | 2790 | 0 |
Python logging LoggerAdapter | 823 | 0 |
Python logging isEnabledFor | 634 | 0 |
Python logging debug warning error | 270 | 0 |
Python logging basicConfig | 511 | 0 |
Python logging Formatter | 712 | 0 |
Python logging FileHandler | 737 | 0 |
Python logging StreamHandler | 410 | 0 |
Python logging exception | 311 | 0 |
Python logging hasHandlers | 661 | 0 |
Python logging addLevelName | 679 | 0 |
Python logging disable | 1925 | 0 |
Comments