|
|
Sometimes you will want to enable verbose logging output for your applications. Instead of using simple `print` statements, we recommend using the [`logging` library](https://docs.python.org/3/howto/logging.html). The library allows categorizing messages into different severity levels and will then only propagate messages above a predefined severity threshold. This threshold can be set at the in the beginning of your program and will automatically propagate to all places where the `logging` module is used.
|
|
|
|
|
|
## Setting the logging level
|
|
|
You can set the logging level like:
|
|
|
```python
|
|
|
import logging
|
|
|
logging.basicConfig(level=logging.[WARNING|INFO|DEBUG|]
|
|
|
```
|
|
|
The first instance of such a code block executed takes precedence over all following calls. Once the level has been set, it cannot be changed again during the same run of the program.
|
|
|
|
|
|
## Using the logging Framework
|
|
|
It is good practice to use a logger instance to generate the messages. This logger instance will make it easier to understand which part of the code the message came from. Using the logger with the `__name__` argument will make it inherit the name of the script in which it was instantiated.
|
|
|
|
|
|
```python
|
|
|
# import
|
|
|
import logging
|
|
|
# get logger with name of module
|
|
|
logger = logging.get_logger(__name__)
|
|
|
|
|
|
# print logs
|
|
|
logger.info("Information")
|
|
|
logger.warning("Warning")
|
|
|
logger.debug("Debug message")
|
|
|
|
|
|
``` |
|
|
\ No newline at end of file |