How to use the Python Logging module – with example

  • Reading time:17 mins read

What is Python logging used for?

Python logging is a means of tracking events that occur whenever a software program is running mostly for purposes of debugging.

Alternatively, we could print statements and then redirect them to a file in Python to determine if a program is executed as intended, however, using print statements may prove to be ineffective, especially when dealing with large and complex programs. 


Python logging module

Python provides a built Python logging module that allows us to pass status messages about the program that is running to any output stream. This program provides different levels of log messages namely: Debug, info, warning, error and critical.


python logging

Debug level

The debug level gives the programmer detailed information about what is happening in a program allowing the programmer to diagnose a problem if any. However, at the debug level we will get no output in the terminal.


Info level

The info level will also get us no output in the terminal.


Warning level

But if we move to the warning level, we will get a message in the terminal letting us know that this is the warning level. We can choose what to display depending on what happens within our code.


Store logging messages

However, we intend to store all the logging messages in a file. To do that we need to do some basic configurations. We then need to do loggin.basicConfig and then specify the name of the file where we intend to store our logs information.

We are going to name this one example.log, we also need to specify the level at which we intend to log at.



Log messages will be appended to the file example.log that has just been created every time we run the program.


Now, every time we log a message in our program it is going to be appended to the file example.log. This allows us to collect in-depth logs about what our program is doing.


As shown in the output above the logs have been created in the example.log file.

Adding a date to our log files

A lot of people prefer to add a date to each log which is okay, that can be accomplished by passing the format attribute as a parameter to the logger as shown below.


Example 2

To elaborate more on the use of the logging module we are going to create a list of dictionaries of animals.



We can use indexing to access any of the elements of the dictionary as shown below.



However, if we try to search for an element with an index out of the range of the dictionary, for instance, the third index we’re going to get an error because the list index is out of range.


Logging error messages

So what if we wanted to log this error into our file and store it there. Let’s say perhaps this was part of a larger program, logging this error into a file will give us the opportunity to know where the program is failing.


Python logging

Using try-except blocks

We can then put this code into a try and except block, this will allow the program to raise an exception in case the code does not execute and then log the error into the file ‘example.log’.


Advanced Python logging

We can also log what was happening beforehand by adding another print statement and logging the message to our file for recording and using f string to let us display the data and say that we are looking for names in that list of dictionaries.


This is particularly useful when working with large files and we’re trying to figure out where things might be failing. Or when trying to log bits of information like we have done in the case above.


If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.