Escribe para buscar…

logging

Esta página todavía no se ha traducido — se muestra en su idioma original:English

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:

python
import logging

logging.debug("A debug message")
logging.info("An info message")
logging.warning("A warning message")
logging.error("An error message")
logging.critical("A critical message")

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 message

Notice 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>
Task
  • Change log level to INFO using logging.basicConfig() so that all log messages with a severity level of INFO and above are displayed.

  • Change log level to ERROR usin g logging.basicConfig() so that only log messages with a severity level of ERROR and 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