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.
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.
The info level will also get us no output in the terminal.
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.
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.
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.
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.