logging
Introduction
Logging is essential for building dependable software. It records software events, creating an audit trail that details various system operations. This trail helps diagnose issues, understand runtime behavior, and analyze user interactions, offering insights into design decisions.
logging
Python’s built-in logging module is incredibly powerful and boasts the necessary logging features that one might require from a separate logging framework. Therefore, it remains the most popular way to log in the Python ecosystem despite the existence of a few third-party logging frameworks like Loguru.
Providing a logging module in the standard library ensures that all Python programs can benefit from having consistent logging features, making it easier to adopt and understand when working on different projects.
To get started with the logging module, you need to import it to your program first, as shown below:
When you execute the above program, you should observe the following output:
WARNING:root:A warning message
ERROR:root:An error message
CRITICAL:root:A critical messageNotice that the debug() and info() messages are missing from the output while the others are present. This is due to the default log level configured on the logging module, which we will get to shortly. For now, understand that this is the intended behavior of the example.
In each record above, you will observe that the log level (WARNING, ERROR, etc.) is printed in all caps followed by root, which is the name of the default logger. Finally, you have the log message that was passed as an argument to the function.
This output format is summarized below:
<LEVEL>:<name>:<message>-
Change log level to
INFOusinglogging.basicConfig()so that all log messages with a severity level ofINFOand above are displayed. -
Change log level to
ERRORusin glogging.basicConfig()so that only log messages with a severity level ofERRORand above are displayed.
Estás leyendo una vista previa.
Inicia sesión para leer el artículo completo. Cualquier cuenta abre 4 artículos gratuitos al mes; el alumnado y el profesorado leen las páginas de su curso sin límite.
Iniciar sesión