Story of Logging -Part1

#logging #severity-level #info #critical #warning

Swati Pandey Apr 29 2021 · 3 min read
Share this

Logging in Python

"Once upon a time there lived a person his name was Logging, he had a very good habit of writing everything he do, he saw, et cetera in his journal. He.."

What is logging ?

Logging is a way to record every event a program undergoes when it runs. In other words it captures the flow of any program and can keep a record of every observation. These records are called LOGS. For example, which IP address has been used to access the program, which user has used the application, in case of error it captures information like which line of code has generated the error, state of program before coming to any particular line of code, etc.

What is the importance of creating logs? Or what is the importance of logging?

Debugging becomes easy if we know exactly where the error is, it becomes  time effective and easy to debug. If in case logging is not maintained then it would be very difficult and time consuming process to locate the position of error in any complex program.

Logging also makes it easy to analyse the performance of the program and redevelop (if that is any word)/scale it up for future.

Module for Logging in Python

With time Python has advanced itself and now it has become very easy to create logging as python provide an already existing module LOGGING.

import logging

This built in module allows to write the logs/messages to a file or to any other output stream. This file can also be used for future reference as it stores the records of the required and desired messages with required details like timestamp, line of code where the error has occurred, etc.

Basic Configuration of Logging 

Logging can be configured based on requirements like whether the programmer wants the messages to get stored in any specified file or to any output stream, what should be the level of the logging(read next section for levels in logging),etc.

  •  filename: records the log in the specific filename given, a FileHandler is created instead of StreamHandler
  •    filemode: it opens the file in the mode provided, by default it is “a” which stands for “appends”
  • format: uses the specified format to format the string for the handler
  • datefmt : uses the specified for date/time representation
  • level : decides the severity level for logging(covered in next section)
  • Note: there are some other keyword arguments you can read the official documentation of logging module. (here---https://docs.python.org/3/library/logging.html)

    Levels in Logging

    In programming world every single execution results special type of events and logging records messages associated to these events and these events are categorised into levels, one below the other. There is not any limit to these levels, developers can create levels as required. Howsoever By default in logging module there are 5 levels of logging also known as Severity of Levels.  They are listed below with increasing severity:

  • debug : It allows logging to record operations in progress of any program and monitor the changes especially in case of any expected potential  error. If level is set to debug then in that case it will capture messages associated to all default levels
  • info: It records the message when something expected happens. If level is set to info it will record all the messages associated to all default levels except debug.
  • warning: It records the message when something unexpected happens and this could be a problem in future but the program continues to work. By default the level is set to warning, if you do not explicitly set level to any other level.
  • error: It records messages when the program stops at any point and is unable to execute further line of codes
  • critical :  It records error when there is some kind of alert in monitoring system.
  • Note: Also check fatal level and  trace level.

    CODE SNIPPETS

    Following code snippets will create

    (i).logging to Console: It specifies StreamHandler and output in this case will go to console.

    Note: By default basicConfig() creates logging to Console and the default logger is root which comes just before the message , root is the name given to the default logger by the logging module.

    import logging
    
    logging.basicConfig(level = logging.WARNING)
    logging.warning("This will go to StreamHandler, Severity level is warning")
    import logging
    
    logging.basicConfig(level = logging.WARNING)
    logging.warning("This will go to StreamHandler, Severity level is warning")
    

    (ii). Logging to a file: It species FileHandler if filename keyword argument is used. This makes the program to create log to a file with the name “test3.log” in append mode which is the default filemode.

    Note: basicConfig() can be set only once  for the root logger so everytime you are trying to change something Kernel need to get restarted again

    import logging
    
    logging.basicConfig(filename = "test3.log", level = logging.WARNING)
    logging.warning("This will go to FileHandler, Severity level is warning")

    (iii)Format and dateformat: Date can be represented in many ways- dd-mm-yy or mm-dd-yy or dd-mm-yyyy or weak of the year etc.

    There is another documentation for date and time format (here--https://www.w3schools.com/python/python_datetime.asp)

    import logging
    
    logging.basicConfig(filename = "test3.log",format = "%(asctime)s - %(message)s",datefmt = "%d-%b-%y")
    logging.warning("This will go to FileHandler, Severity level is warning")

    (iv). Level: Programmer can create level as per requirement or can use the default levels. Following code snippet specifies severity levels to StreamHandler.

    import logging
    logging.debug("DEBUG")
    logging.info("INFO")
    logging.warning("WARNING")
    logging.error("ERROR")
    logging.critical("CRITICAL")

    Note: debug () and info() don’t get logged as Default severity level is “warning” or above. To log messages associated to “debug” or “info” level following code snippet can be used:

    import logging
    logging.basicConfig(level = logging.DEBUG)
    logging.debug("DEBUG")

    Implementing Logging in a Simple Program

    It is a good practice to create logging even in your simplest program. Let us see how to implement.

    def division(denominator):
        #importing Logging module
        import logging
        
        try:
            result = 8/denominator
            #configuring to Info Level
            logging.basicConfig(level = logging.INFO, format='%(asctime)s - %(message)s')
            logging.info("division of 8 by {} is done and the result is {}".format(denominator, result))
        except Exception as e:
            #logging the error
            logging.error("There is some problem due to which try block couldnt be executed and error has occuured which is %s",e)
        else:
            #this will get executed only if try runs successfully
            logging.info("Programm is running smooth")
        finally:
            logging.info("End of the program")
            
        
    #calling the function    
    division(0)

    Note: This article covers the basic understanding of logging in Python along with  its importance and implementation at basic level

    Comments
    Read next