Python Easy Logging Tutorial
In this tutorial, we'll show you how to log messages using Python's built-in logging module. We'll also cover some advanced features like filtering and customizing output.
The Python logging module is a ready-to-use and powerful module designed to meet the needs of beginners as well as corporate teams. It is used by most third-party Python libraries, so you can integrate your log messages with those of these libraries to produce a consistent log for your application.
Adding logging to your Python program is as simple as that:
import logging
With the imported logging module, you can use something called a "logger" to record the messages you want to see.
By default, five standard levels are indicating the severity of events. Each has a corresponding method that can be used to record events at that severity level.
The levels defined, in order of increasing severity, are as follows:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
Log your first messages
The logging module provides you with a default logger that allows you to get started without having to do a lot of configuration. The corresponding methods for each level can be called up as shown in the following example:
import logging
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
The output of the above program would look like this:
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
The output displays the severity level before each message with + root +, which is the name that the logging module gives to its default logger. (Recorders are described in detail in later sections.)
This format, which displays the level, name, and message separated by a colon (+: +), is the default output format that can be configured to include items such as timestamp, line number, and other details.
Note that the messages debug and info have not been recorded. Indeed, by default, the logging module records messages with a severity level of warning or higher. You can change this by configuring the logging module to log events from all levels if you wish. You can also set your own severity levels by modifying the configurations, but this is generally not recommended as it can lead to confusion with the logs of some third-party libraries you may be using.
Change logger level
In order to see the debug level messages, we have to set the logger to a level lower or equal to the debug level. In order to do so, we just need to:
import logging
# Set debug level
logging.basicConfig(level=logging.DEBUG)
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
The output of the above program would look like this:
DEBUG:root:This is a debug message
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
Change logger message
You can easily add more information in your logs such as the time, the logger name, etc ...
import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')
The output of the above program would look like this:
2020-06-16 18:30:31,137 is when this event was logged.
To change the default date format:
import logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG, datefmt='%m/%d/%Y %I:%M:%S %p')
which returns the following output
06/16/2020 06:32:16 PM is when this event was logged.
Save logs to file
One of the most powerful feature of logging is that you can easily save the logs into a file.
import logging
logging.basicConfig(filename='example.log', format='%(asctime)s %(message)s', level=logging.DEBUG, datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')
This will append the logs to the example.log
file.
Have fun logging !