Ticker

6/recent/ticker-posts

Logging in Python | logging levels -Datacloudy

 


In this blog we are going to see about Logging in python. It is the most important thing for a python developer.  Logging is generally used to debug your program. Debug is nothing but a feature through which we can track our execution of our program.

Generally hardest part of fixing a problem is to identify where the problem is located. Logging is helpful when we are trying to figure out which section of our code is causing an issue or the output is not as expected.

You may think that we can directly use print statement for this. Yes we can use print statement, but it has some disadvantages.  When we complete the code in development stage using print statement for debug, and while pushing the code to production we need to delete those print statements used for debug as that is not needed in production and it looks junk detail in production perspective. We also having an advantage of using logging that we can actually print the logging details in a separate logging file instead of console.

We can now see this with an example.

The first example is without logging,

def namecheck(name):
    if  len(name)<4:
        print(“checking length”)
        return(“invalid name”)
    else:
        print(“ inside else part”)
        return (“valid name”)

 

We can see in the above example we used print statement to get the information of the execution of the program in each part of the code.

Now let us see the same example with logging, Here first thing we need to import the logging module.

import logging

logging.basicConfig(level=logging.DEBUG) 

def namecheck(name):
    if  len(name)<4:
        logging.debug(“checking length”)
        return(“invalid name”)
    else:
        logging.debug (“ inside else part”)
        return (“valid name”)

In the above example we can see that we used logging.debug() instead of print statement to get the information of the execution of the program in each part of the code.


Actually both the example will give same result, but the logging example will give the details of logging information as well in the console.

We can also write the logging result in a separate file as well,


import logging

logging.basicConfig(filename=’example.log’, level=logging.DEBUG)

def namecheck(name):
    if  len(name)<4:
        logging.debug(“checking length”)
        return(“invalid name”)
    else:
        logging.debug (“ inside else part”)
        return (“valid name”)

 

To remove the logging result in production, before deploying the code we can go for disable feature of logging , lets see an example using the same code in the above examples

 

import logging

logging.basicConfig(filename=’example.log’, level=logging.DEBUG)

logging.disable( )

def namecheck(name):
    if  len(name)<4:
        logging.debug(“checking length”)
        return(“invalid name”)
    else:
        logging.debug (“ inside else part”)
        return (“valid name”)

 

From the above code we can see that, we don’t need to comment the logging information to to deploy it in to production,  instead of commenting we can use the logging.disable() which will take care of that part.

 

                    In python we have five different logging level and they are,

  • DEBUG

  • INFO

  • WARNING

  • ERROR

  • CRITICAL

 

Now let us see brief information of each of the level quickly,

      1) DEBUG:

It is the lowest level and it is used to record the simple details.
The  value of this logging level is 10

Logging function:
             
logging.debug()

 2) INFO:

It is used to record general information.
The  value of this logging level is 20

Logging function:
             
logging.info()

 

3)WARNING:


It is used to record the potential issues which may not cause errors in the future.
The  value of this logging level is 30

Logging function:
           
 logging.warning()

 

4) ERROR:


It is used to record the error which cause a section of the code to fail.
The  value of this logging level is 40

Logging function:
           
 logging.error()

 

5) CRITICAL:


It is the highest level and It is used to record the blocker which fails your program.
The  value of this logging level is 50

Logging function:
           
logging.critical()


Thus in this blog, we saw about logging in python and its different level in crisp and clear manner. Hope this information is helpful. If you like this blog and want to show some support please follow our blog by clicking follow button on the top right of this screen.

Thank You!!! 


Post a Comment

0 Comments

Ad Code