Logging is a powerful way to help us develop a better understanding of the flow of our program while opening up ways to see different perspectives in the way we developed our code. Loggins is a method by which we can track different events that happen when our program is being executed.
An event is explained with an expository message of what happened and can also contain some variables, specifically, these are to depict the value of the variable amongst different occurrences of the event. A log can convey valuable information such as which user used an application or even which IP address used an application.
Other use-cases would be to display console output (using print), to report different events that take place in the usual flow of a program, to report an error that is not displayed, etc.
Logging our code at the right places can help us not just debug errors, but perform analysis on the performance of our program in terms of speed and we can also use logging to create strategies on how to market our program. Because of all this, Python has provided its standard library to log our application, and we will be working with this library in our blog.
The standard Python module is designed to be used by both ends of the spectrum, for beginners and professional developers. Within the logging module, the functions are named to indicate the level of severity for differing events. The functions in the increasing order of their severity are:
When we want to track an event, there are multiple ways to go about it but the simplest and quickest way would be to print them onto the console.
Let’s consider a simple script that utilizes each of these functions.
import logging logging.debug('Message is from a debug log. Severity level: 10') logging.info('Message is from an info log. Severity level: 20') logging.warning('Message is from a warning log. Severity level: 30') logging.error('Message is from an error log. Severity level: 40') logging.critical('Message is from a critical log. Severity level: 50')
When we run this, we get:
Since we have not configured our logging functions to do anything specific, it currently is the same as replacing the print statement with a logging function.
We can also observe that the first two levels of severity, i.e., debug and info respectively have not been logged onto the console. This is because by default the configuration is that Python would only display log messages of severity level 3 or more i.e., from warning onwards. We can modify these to our liking if required, and we also have the option of setting our severity level but this is not a recommended practice as it conflicts with logs of any external libraries we might be using.
Another thing we can observe is ‘root’, which is the name given to its default logger by the logging module.
The output has been formatted to show the level, name and message which are separated by a colon. However, this can also be altered to include other details like timestamp, line number, etc.
What have we learned?
- What is logging?
- What are some uses of logging?
- Which module can we use to create logs in Python?
- What are the different names and severity levels of logging functions?
- Which logging functions are omitted by default and why?
- What all can we include we displaying our log?