Logging

Logging is a very useful 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.

Logging in Python

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 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 especially 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:

  • NOTSET=0 : Nothing is logged
  • DEBUG=10 :These are used to give Detailed information, typically of interest only when diagnosing problems.
  • INFO=20 : These are used to Confirm that things are working as expected
  • WARN=30 : These are used an indication that something unexpected happened, or indicative of some problem in the near future
  • ERROR=40 : This tells that due to a more serious problem, the software has not been able to perform some function
  • CRITICAL=50 : This tells serious error, indicating that the program itself may be unable to continue running

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')

Configuring Logging

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 very simple.

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:

  • level: The root logger will be set to the specified severity level.
  • filename: This specifies the file.
  • filemode: If filename is given, the file is opened in this mode. The default is a, which means append.
  • format: This is the format of the log message.
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()
logging-python-selenium-logger

Logging with selenium python

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()

Logging to console with python

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) 

Print vs Logging

The logging package has a lot of useful features which are missing in print statements:

  • Easy to see where and when (even what line no.) a logging call is being made from.
  • You can log to files, sockets, pretty much anything, all at the same time.
  • You can differentiate your logging based on severity

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group