In the official documentation there is a Basic Logging Tutorial.
A very simple example would be:
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
That's the explanation:
First you import logging
, and after that you configure it with basicConfig
indicating the file that you want to save the log messages to.
Then, every time you want to add something to the log, you have to use the logging functions: debug()
, info()
, warning()
, error()
and critical()
, that are named after the level or severity of the events they are used to track.