An Introduction To Logger in Python
Posted By : Anoop Sharma | 09-Mar-2021
Logging is a valuable piece of code in a software engineer's tool collection. It can assist you with building up a superior comprehension of the progression of a program. It also helps you find a scenario that you probably won't have thought of while creating. This blog post sheds light on the importance of logging in Python application development.
Logs give developers an additional arrangement of eyes that are continually taking an observation at the stream that an application is experiencing. They can store data, similar to which the client or IP got to the application. If a mistake happens, at that point they can give a larger number of bits of knowledge than a stack follows by mentioning to you what the condition of the program was before it landed at the line of code where the error occurred.
By logging helpful information from the correct spots, you can troubleshoot errors effectively as well as utilize the information to break down the working of the application to get ready for scaling or see utilization examples to anticipate showcasing.
Python gives a logging framework as a piece of its standard library, so you can rapidly add logging to your application. Right now, will see why utilizing this module is the most ideal approach to add logging to your application just as how to begin rapidly.
Logging Module
The logging module in Python is a prepared to-use and powerful module that is intended to address the issues of learners as well as big business groups. It is used by the vast majority of the third-party Python libraries, so you can incorporate your log messages with the ones from those libraries to deliver a homogeneous log for your application.
Adding logging to your Python program is as simple as this:
import logging
With the logging module imported, you can utilize something many refer to as a "logger" to log messages that you need to see. As a matter of course, 5 standard levels are demonstrating the seriousness of occasions. Every ha a comparing technique that can be used to log occasions at that degree of seriousness. They are listed below:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
import logging
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
The output of this would be as:
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
Each message has its severity level associated with it. Notice that, INFO and DEBUG log was not displayed. This is because, by default, the logging module logs only the messages with a severity level of WARNING or above and if you want to display the above logs also then you need to change some default settings.
Conclusion
Adding a logger in your project would increase the understanding and maintainability of the code that you are writing. Hence Everyone should use some kind of logger in their project.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Anoop Sharma
Anoop is a Python developer, who has worked on Python Framework Django and is keen to increase his skillset in the field. He has a zest for learning and is capable of handling challenges. He is a team player and has good enthusiasm.