Logging is a handy tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.
Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application.
If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred.
By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.
Python provides a built-in library called logging to make it easy to add logging to any module or application in a unified way.
The logging module is part of the standard Python library and provides tracking for events that occur while the software runs. You can add logging calls to your code to indicate what events have happened.
The logging module allows for both diagnostic logging that records events related to an application’s operation, as well as audit logging which records the events of a user’s transactions for analysis. It is primarily used to record events to a file.
Python Logging Levels: The log level corresponds to the "importance" a log is given: an "error" log should be more urgent than the "warn" log, whereas a "debug" log should be useful only when debugging the application.
There are six log levels in Python; each level is associated with an integer that indicates the log severity:
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 main application should configure the logging subsystem so log messages go where they should. The Python logging module provides a large number of ways to fine-tune this, but for almost all applications, the configuration can be straightforward.
In general, a configuration consists of adding a Formatter and a Handler to the root logger. Because this is so common, the logging module provides a utility function called basicConfig that handles a majority of use cases.
Applications should configure logging as early as possible, preferably as the first thing in the application, so that log messages do not get lost during startup.
Finally, applications should wrap a try/except block around the main application code to send any exceptions through the logging interface instead of just to stderr.
Some of the commonly used parameters for basicConfig() are the following:
Let's see an example program.
import unittest
import logging
class Test(unittest.TestCase):
#Create and configure logger
logging.basicConfig(filename="newfile.log",
format='%(asctime)s %(message)s',
filemode='w')
#Creating an object
logger=logging.getLogger()
#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
def test_read_excel_column_file(self):
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')
if __name__ == "__main__":
unittest.main()

In similar ways, we can use the logger along with selenium python.
import unittest
import logging
class Test(unittest.TestCase):
#Create and configure logger
logging.basicConfig(filename="newfile.log",
format='%(asctime)s %(message)s',
filemode='w')
#Creating an object
logger=logging.getLogger()
#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
def test_read_excel_column_file(self):
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')
if __name__ == "__main__":
unittest.main()
To log the statement to the console, you just need to remove the fileName option in the basicConfig.
import unittest
import logging
class Test(unittest.TestCase):
#Create and configure logger
logging.basicConfig(filename="newfile.log",
format='%(asctime)s %(message)s',
filemode='w')
#Creating an object
logger=logging.getLogger()
#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
The logging package has a lot of useful features which are missing in print statements:
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.