Be taught Python – Begin with logging module in Python

If you are dealing with nested codes and complex logic then logging comes first in mind when you think of debugging your code. logging in python will let you print command line logs as well as let you create log file and write your all logs to deal with them later.

logging module of python has numerous of easy method that makes our work so easy. It easily let you set the structure of log lines with help logging module’s basicConfig() method.

So log can either be error message or Information or just a warning for making this thing easier logging module contain five logging levels that you can set as to specify your log message as one of:

  • DEBUG ( 0 )
  • INFO ( 1 )
  • WARNING ( 2 )
  • ERROR ( 3 )
  • CRITICAL ( 4 )
To set logging level you have to pass it inside basicConfig() method’s level attribute, like to set logging level as debug message we have to pass it like:
or, either you can pass the number value corresponding to it.

To set log file name that you want to generate you have to pass filename to  basicConfig() like this:
The code below contains the program that store employee name and as employee get added it will write a Info log to the log file that we create by passing the value of filename in basicConfig method.

**Code above will generate a log file of name test.log in your working directory. 

Python Programs On Demand ‘Free’