Producing logs in Python
Python includes a standard module to produce logs. This module is easy to use, with a very flexible configuration, but it can be confusing if you don't understand the way it operates.
A basic program to create logs looks like this. This is available as basic_logging.py
on GitHub at https://github.com/PacktPublishing/Python-Architecture-Patterns/tree/main/chapter_12_logging:
import logging
# Generate two logs with different severity levels
logging.warning('This is a warning message')
logging.info('This is an info message')
The .warning
and .info
methods create logs with the corresponding severity message. The message is a text string.
When executed, it shows the following:
$ python3 basic_logging.py
WARNING:root:This is a warning message
The logs are, by default, routed to stdout
, which is what we want, but it is configured not to display INFO
logs. The format of the logs is also the default, which doesn&apos...