Quick Basics of Logging in Python

Rijvi Zaman
Big0one
Published in
5 min readJun 14, 2020

Logging allows you to the document flow of the program by having logging statements that can be complex to find out the bugs that are inserted various parts of the program and having that information from those logging statements either directed to your console or log files.

Photo by Chris Ried on Unsplash

these statements allow you to debug your program on a higher level and help you catch bugs that are not caught by the compiler or at runtime and its not only help you when you want to debug your program or your application but also it can keep track of your program and maintain properly.

Logging module

Python’s logging module is to help us to get the right way to review and check the program to be bug-free. This module can print information to the console that can make our time precious and valuable. This is a set of functions that the developers can log not only to the console but also to any open filehandle.

There are a few categories for the python’s logging module which the developer uses easily.

Python uses the following priority levels, starting with the lowest priority first then the highest:

Debug

Info

Warning

Error

Critical

Python’s Logging Module

By default, the Python logging module is set at the warning logging level. This means that any requests at the warning level or higher priority are logged to the destination. The rest are ignored.

Configuring the Logger

There are four different abstractions: loggers, handlers, filters, and formatters. They are described given below:

Logger: Loggers expose the primary interface that we use to log events from the application. This is the class whose objects will be used in the application code directly to call the functions.

Handler: Handle where the log event is sent. Using different handlers, we can have log events that go to different files or some to one file and the rest to Syslog. Handlers send the LogRecord to the required output destination, like the console or a file. The handler is a base for subclasses like StreamHandler, FileHandler, SMTPHandler, HTTPHandler, and more. These subclasses send the logging outputs to corresponding destinations, like sys.stdout or a disk file.

Filters: We can change what log events get written or add contextual information to an event by using a filter

Formatter: Log formatters allow us to specify the layout of our data when it gets written. This is where you specify the format of the output by specifying a string format that lists out the attributes that the output should contain.

Logger Design

To import all the functions Adding logging to your Python program is as this:

import logging

After import logging you can use all the methods or functions to implement in your code.


#Logs a message with level DEBUG on the root logger.
logging.debug('This is a debug message')
#Logs a message with level INFO on the root logger.
logging.info('This is an info message')
#Logs a message with level WARNING on the root logger.
logging.warning('This is a warning message')
#Logs a message with level ERROR on the root logger.
logging.error('This is an error message')
#Logs a message with level CRITICAL on the root logger.
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

Here the debug and the info messages didn’t get logged because of the level of severity. By default, the logging module logs the messages with a severity level of warning or above. You can change that by configuring the logging module to log events of all levels if you want. You can also define your own severity levels by changing configurations.

The output of the program

Here WARNING/ERROR/CRITICAL is called level and root is called name and rest are the message that we want to display now the root is the logger object that’s the default logger object now with logging we can create multiple logger objects with different levels and different formats.

Formatting the Output

Now that we have our log messages being written to a persisted medium, we need to make our messages more useful.

Let’s configure a formatter to create a better layout and to include additional information for helping with debugging our application. If you want to log the process ID along with the level and message, you can do something like this:

Configure the formatter

%(asctime)s adds the time of creation of the LogRecord. The format can be changed using the datefmt attribute, which uses the same formatting language as the formatting functions in the date-time module, such as time.strftime():

Formatted Output

There are different formats you can use. Here this format will be processed which is the process ID of the Python program the level name and message so logging with process format if you run this you’ll see this is the process ID with the warning which is along with the level name and the message logging with process format and here I have one more decoration using date-time you could also use date-time which I think is probably one of the most useful formats because you know you want to log the time and the date where certain events occurred so here the format is ASCII time and I think that’s ASCII time format the message better and here’s how you are formatting the time where there is a month, a day, a year.

Logging frameworks are a powerful and necessary tool. There’s a lot more we can do with our logging module, such as configuring logging for multiple modules or using a configuration file. We also need to decide on an overall logging strategy.

There are far more options that can be used to configure the logger, handlers, and formatters. I encourage you to experiment and see what works best for your particular application.

References:

[1]: https://docs.python.org/3/library/logging.html#module-logging

[2]:http://nathanielobrown.com/blog/posts/quick_and_dirty_python_logging_lesson.html#:~:text=The%20absolute%20basics,line%20number%20and%20module%20name

[3]: https://realpython.com/python-logging/#using-handlers

[4]: https://www.scalyr.com/blog/started-quickly-python-logging/

--

--