Written by Nitish Jha
Hello everyone, this article is going to be a little shorter than usual but will surely give you the knowledge for which you came here.
Today, I am going to talk about logging, one of the most underrated concepts that is often ignored while learning to code and that can help you to revamp your experience with your code even after you have deployed it in production.
Now, you may be wondering how it can help you?
So, without wasting any time let me explain!
Every program or application faces a situation where it is not able to perform as expected from it and in that case, the programmer should get a warning to fix the issue before it’s too late and should be informed about the condition in the case of a system crash.
For this purpose, a programmer uses logs (records in simple language). Every activity of a program is logged (recorded) which will tell the programmer about the state of the program that helps the programmer to understand all the issues and avoid any errors.
One might compare logs with the messages printed on the console when faced by any error but in a large scale application, these messages might become confusing. In logs, we can outsource these messages and even categorize them based on priority (security levels).
Let’s discuss the security levels of the messages. These are categorized based on priority because some messages are urgent as they tell about any critical failure and some messages just provide information. In Python, we have five security levels (mentioned in the least to the highest priority order):
Debug: The main purpose of these messages is to find errors, testing your code, and do some experiments with it.
Info: These messages are used to keep track of all the major events that have occurred in a program and informs that everything is working as expected. For eg: “Picture uploaded successfully.”, “Payment successful!”, etc.
Warning: These messages are used to inform us about things that might cause a problem in the future. For eg: “Low disk space.”, “Running older software version.”, etc.
Error: These messages are used to inform about events that didn’t go according to plan and the program was not able to perform certain functions.
Critical: These messages inform about critical events that might have lead to failure or system crash.
We get all the messages of the levels above the level that we have selected.
For example: If we select warning level messages to be logged then we will also get the error messages and critical messages in the log.
Well, enough of the theoretical part and let’s get our hands dirty in actual code.
Create a new python file and import the logging module.
Now, create and configure the logger where we will define the format of our log messages and define the security level (by default it is set to the warning level). We want the log messages to be logged in a file so will pass the file name also else these messages will show on the console just as a print statement and if the file doesn’t exist yet then it will create one for us.
logging.basicConfig(level=logging.DEBUG, filename=”logfile.log”, filemode=”w”, format=”%(asctime)s:%(levelname)s:%(message)s”)
More about logging format here: https://docs.python.org/3/library/logging.html#logrecord-attributes
Let's get our hands little dirtier...
Create a logger object
myLogger = logging.getLogger()
Now, we can test out log messages.
myLogger.debug(“Testing the code.”) myLogger.info(“Everything is going as expected.”) myLogger.warning(“This is a warning.”) myLogger.error(“Not able to perform the function, Recheck!”) myLogger.critical(“No network connection found. Check connection settings.”)
Run the code and you will see a log file, open it with any text editor and you can see your log messages. Try changing the security level and rerun the program, experiment with the format of the log messages.
So, we have covered the basics of logging. I hope this helps you improve your professional programming experience. There are a lot more things we can do in logging that we will discuss in the future till then keep converting coffee into bugs good quality code ;)
About the person behind the keyboard: Nitish is a passionate Machine Learning Engineer and an amazing blogger. He is currently pursuing B.Tech. If you guys want to contact him, just click on his name.