Logging in Python
Logging in python is done using the built-in library called logging
in python. Logging is one of the most important factor in a good software development cycle. It helps you to document the code from the developer’s perpective. Logs that are generated using the logging module gives an extra set of eyes to the developer to better understand the unexpected behaviour of the code. In a simple way it is like you are leaving signs or mark for every place that you are visiting. Not only it will help you to remember, It will also help others to get to you easily if you get into any sort of trouble.
Today we will implement logging with a small python code to show its working and how you can easily get started with writing meaningful and imports logging messages. And if you are just begining your programming career with python I would suggest you go through the Basics of Python first and other articles as well.
Code Implementation
Now let’s write some code to implement logging module
Open an editor of your choice and make sure you have python installed on your system if not then download and install python from here .
Create a file and name it as logs.py
and write down the following code in it
#importing logging module
import logging
#Configuration of your log file is being done here
logging.basicConfig(filename="project_name.log",
format='%(asctime)s %(message)s',
filemode='w')
#Create an object to access anywhere in the code
logger=logging.getLogger()
#Changing the level of logs to be saved
logger.setLevel(logging.DEBUG)
#Test messages
logger.debug("Simple Debug Message")
logger.info("Any Information of the Code")
logger.warning("Warning Alert: Something not good")
logger.error("An error Occured!!")
logger.critical("Aliens are here, I repeat Aliens are here!!")
Code language: Python (python)
Run the file from your terminal using python file_name.py
in your terminal. You would see that a new file has been created. In my case project_name.log
is created next to my logs.py
with the output as given below
2021-11-07 00:06:01,616::DEBUG - Simple Debug Message
2021-11-07 00:06:01,616::INFO - Any Information of the Code
2021-11-07 00:06:01,616::WARNING - Warning Alert: Something not good
2021-11-07 00:06:01,617::ERROR - An error Occured!!
2021-11-07 00:06:01,617::CRITICAL - Aliens are here, I repeat Aliens are here!!
Code language: Python (python)
Let’s break down the code now
- In the first line, you are importing logging module which is inbuilt in python in your code so that you can use it’s functionality.
- Second steps involves with the configuration, which basically helps you to set a name to the log file in which all the logs will be saved. The format in which you want to save your log entries. You can also set the filemode which basically is whether you want to append the logs everytime you run the code or write new logs erasing the previous ones.
- In the third step, you are creating a log object which helps you to access all the features of the logging module class in Python. This is very much similar to how you create an object of a class and can access all the methods and variables of that class using that object.
- In the fourth step, we are setting up the log level. Which means the logger object will start saving logs from the mentioned level. By default
logging
module saves logs fromwarning
level. If you comment this particular piece of code you will get an output of this sort.Code language: Python (python)2021-11-07 00:19:41,832::WARNING - Warning Alert: Something not good 2021-11-07 00:19:41,832::ERROR - An error Occured!! 2021-11-07 00:19:41,832::CRITICAL - Aliens are here, I repeat Aliens are here!!
- Next and the last are the ways in which you can define your log messages which will surely improve your code quality and help you detect any error or issue way faster.
Keep Coding and Happy Coding 🙂