Logging with logging in Python
Logging means to track what events happens when you run some software
In Python the easiest way to do that is to use print() statements when your code needs to perform actions that you want to track. If you need some more advanced logging (i.e. save the logs in a file or in a database table) the easiest way is to use logging
Another point to consider is: when do you have to use logging vs raise an exception? In a nutshell as always it depends on what you have to do. How the software is expected to behave? Is the error so critical that further steps cannot/should not be done? In this case the exception is the expected behaviour. Is it ok to go on with the execution of the code even if we’ve got any errors? Ok in this case let’s keep track that we got some errors but let’s continue with the execution of the code. Really interesting discussion on reddit about this topic
Setup for this examples: let’s generate random integers and let’s suppose that the values generated must respect some thresholds which indicate if a value is a warning, an error or a critical error
Let’s consider 3 examples:
- example_1: let’s use print() to log what is happening in the code
- example_2: using example_1 as base let’s use logging module
- example_3: using the example_2 as base let’s track different error levels in different ways. In this example I have also created SQLiteHandler.py to log error messages on a SQL table
Example 1
This is the base example: we are using print() to log every step of the code on the console
Example 2
In this example we use basic configuration of logging to save in a file the steps of the code. The messages have different levels (debug, info, warning, error and critical). By changing the level in the basicConfig() we can change the starting level we log
Example 3
In this example we setup different handlers for the different logging levels and we also use the module SQLiteHandler to log on a SQL table the messages starting from info level
Let’s have a look at the SQLiteHandler module: it is based on sqlite_handler.py from Yarin Kessler. Using an attrirbute_list we can choose which information we want to log (they will be the columns off the log table). In this example I am using SQLite but it is pretty straightforward to set up another handler in the same way for any other database we want to use
Of course if you change the attrirbute_list remember to DROP/CREATE the table or anyway do something to manage this change on the database otherwise when you try to save a record in the log table you will get an error because the columns do not match
If we check the log table we will only see the info, error and critical messages, if we check example_3_error.log we will see only the error and critical messages and if we check example_3_critical.log we will only see the critical messages
Note: Code can be found in this github repository