Blog

  • Logging Infrastructure in Python

    Posted by Rohitesh Rawat | Last Updated: 29-Jun-18

    We use 'print' to print a statement but it just prints to the console.
    Logging allows us to instruct the program to emit information while the system is still running.
    This information can be useful to us.


    >>>Benefits of logging infrastructure are:

    1- Multi-threading support.

    2- Categorization via different levels of logging.

    3-Flexibility and configurability.

    4- Separation of the how from the what.


    >>>Logging levels
    Some of the logging levels are:

    1- DEBUG: it is used when we are debugging something.
    When want to write some detailed instructions on an issue that only you want to see when debugging and no other person should see that.

    2- INFO: it confirms that all the things are working as expected.

    3- WARNING: it indicates that something unexpected happened or may happen in the future, but the software is still working as expected.

    4- ERROR: it indicates a serious problem.
    The software may not able to perform some action.


    >>>Logging Format: it changes the format of log messages that are displayed in the console.

     

    import logging
    
    logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',level=logging.DEBUG)
    logging.warning("warning message")
    logging.info("info message")
    logging.error("error message")
    
    >>>Output will be something like this:
    06/03/2018 11:12:32 PM: WARNING: warning message
    06/03/2018 11:12:32 PM: INFO: info message
    06/03/2018 11:12:32 PM: ERROR: error message
    

     


    >>>Logger Console Example:

     

    import logging
    
    class LoggerConsole():
    
        def testLog(self):
            # create logger
            logger = logging.getLogger(LoggerDemoConsole.__name__)
            logger.setLevel(logging.INFO)
    
            # create console handler and set level to info
            consoleHandler = logging.StreamHandler()
            consoleHandler.setLevel(logging.INFO)
    
            # create formatter
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s: %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p')
    
            # add formatter to console handler
            consoleHandler.setFormatter(formatter)
    
            # add console handler to logger
            logger.addHandler(consoleHandler)
    
            # logging messages
            logger.debug('debug message')
            logger.info('info message')
            logger.warn('warn message')
            logger.error('error message')
            logger.critical('critical message')
    
    demo = LoggerConsole()
    demo.testLog()
    
    
    >>>Output will be something like this:
    06/03/2018 11:12:32 PM -sample_log - INFO: info message
    06/03/2018 11:12:32 PM -sample_log - WARNING: warning message
    06/03/2018 11:12:32 PM -sample_log - ERROR: error message
    06/03/2018 11:12:32 PM -sample_log - CRITICAL: critical message
    

Tags: testing