You can specify a logging level using ‘logging.basicConfig’
method. Following are the built-in logging levels supported by python in
ascending order.
Level
|
Value
|
Description
|
NOTSET
|
0
|
No logging level set.
|
DEBUG
|
10
|
A general debugging event.
|
INFO
|
20
|
An event for informational purposes.
|
WARNING
|
30
|
An event that might possible lead to
an error.
|
ERROR
|
40
|
An error in the application, possibly
recoverable.
|
CRITICAL
|
50
|
A severe error that will prevent the
application from continuing.
|
DEBUG is the lowest built-in severity
level and CRITICAL is the highest built-in severity. . If you set logging level
to WARN, then it enables logging for equal/higher levels like WARNING, ERROR, CRITICAL.
import logging logger = logging.getLogger() def logMessages(): logger.debug("Debug message") logger.info("Information Message") logger.warn("Warning Message") logger.error("Error Message") logger.critical("Critical Message") if (__name__ == "__main__"): logMessages()
Run above
program, you will get following messages.
Warning Message Error Message Critical Message
Observe the
output, it shows only Warning, Error, Critical messages. It is because, the
default level is WARNING. Update the log level to ERROR, you will get only
ERROR, CRITICAL messages.
import logging logger = logging.getLogger() logging.basicConfig(level=logging.ERROR) def logMessages(): logger.debug("Debug message") logger.info("Information Message") logger.warn("Warning Message") logger.error("Error Message") logger.critical("Critical Message") if (__name__ == "__main__"): logMessages()
Run above
program, you will get following messages.
ERROR:root:Error Message CRITICAL:root:Critical Message
No comments:
Post a Comment