Logging Library

Logging library is a very important module in Python for the programmer,it helps to better develop the software in real scenarios.

It provides many features to programmers,log can store the information in a file,which may be related to debug,info and etc . You just only debug your application and you can also check the application flow and performance.

Python Logging Module

Logging module of python is a very powerful library that is designed and beneficial for beginners and developers too. It is used in most Third party libraries. With the help of this you can easily integrate the log message,and also create the log file of your software.

It is the very simple to add log into you programm just importing import logging or from logging import *

After importing the logging library you can use the term name ‘logger’ to log messages that you can see.
Basically it is provides the 5 standard level of indicating the security events, which is follows

  • DEBUG : Details information regarding when diagnose the problem
  • INFO : it is the confirmation the even work according to expectation
  • WARNING : it is the sign that something unexpected happened.

  • ERROR : it is used for more serious problems,that some functions are not working on any software.

  • CRITICAL : It indicate the serious error, program itself unable to running,
import logging
logging.debug('This is DEBUG Message')
logging.info('This is Info Message')
logging.warning('This is Warning Message')
logging.error('This is an Error Message')
logging.critical('This is a Critical Message')

Output

C:\Users\ASUS\Desktop>python test.py
WARNING:root:This is Warning Message
ERROR:root:This is an Error Message
CRITICAL:root:This is a Critical Message

The output shows as severity level before each message along with root,
You notice that the debug and info message did not get logged. This is because by default logging logs the message from the WARNING according to the severity level.

Basic Configuration

The method basicConfig(**kwargs) used for configuring the logging.

Some commonly parameter which is used as followed

level : the root logger is set to be specified according to severity level.

filename: specified the file name.

filemode : file mode we have to specified that what you want to do with the file basically by default is on append mode.

format : this is used for the formatting in log message

Let’s use all the parameters in the programs .

Program

import logging

logging.basicConfig(level=logging.DEBUG)
logging.debug('this is get logged')

Output

C:\Users\ASUS\Desktop>python test.py
DEBUG:root:this is get logged

Now we will using the filename and the filemode
Program

import logging

logging.basicConfig(level=logging.DEBUG,filename="test.log",filemode='w')
logging.debug('this is get logged')

Output

Now will use the format

1 Programs

import logging

logging.basicConfig(level=logging.DEBUG,format='%(name)s - %(levelname)s - %(message)s')
logging.debug('this is get logged')

Output

C:\Users\ASUS\Desktop>python test.py
root - DEBUG - this is get logged

2 Program

import logging

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.WARNING)
logging.warning('you want to save password')

Output

C:\Users\ASUS\Desktop>python test.py
2020-09-16 20:14:41,982 - you want to save password

%(asctime)s is used for the creating the time date at log file.

When to use logging

Logging provides the set of convenience functions for simply the different tasks,which are the debug(),info(),warning(),error() and critical() so determine their work see the table.

Task Best Tools
Display on console output for basic use of command line and script. print()
Report event which is occur during normal operations logging.info(),logging.debug() is best for detailed diagnosis reports.
Generate a warning regarding any particular task. logging.warning()
Report error regarding particular runtime event. Use for Raise an exception
Report suppression of error without raising the exception. logging.error(),logging.critical()
Level Numeric Value
NOTSET 0
DEBUG 10
INFO 20
WARNING 30
ERROR 40
CRITICAL 50

Logging.NOTSET

#importing module 
import logging 
  

logging.basicConfig(level=logging.NOTSET) 
#all message will be print
#Test messages 
logging.debug("its is safe") 
logging.info("it is a information") 
logging.warning("it is a warning") 
logging.error("I think file not found") 
logging.critical("Internet is not working")

Output

C:\Users\ASUS\Desktop>python cmp.py
DEBUG:root:its is safe
INFO:root:it is a information
WARNING:root:it is a warning
ERROR:root:I think file not found
CRITICAL:root:Internet is not working

Logging.DEBUG

#importing module 
import logging 
  

logging.basicConfig(level=logging.DEBUG) 
  
#Test messages 
logging.debug("its is safe") 
logging.info("it is a information") 
logging.warning("it is a warning") 
logging.error("I think file not found") 
logging.critical("Internet is not working")

Output

C:\Users\ASUS\Desktop>python cmp.py
DEBUG:root:its is safe
INFO:root:it is a information
WARNING:root:it is a warning
ERROR:root:I think file not found


CRITICAL:root:Internet is not working

Logging.INFO

#importing module 
import logging 
  

logging.basicConfig(level=logging.INFO) 
  
#Test messages 
logging.debug("its is safe") #This will not print
logging.info("it is a information") 
logging.warning("it is a warning") 
logging.error("I think file not found") 
logging.critical("Internet is not working") 

Output

C:\Users\ASUS\Desktop>python cmp.py
INFO:root:it is a information
WARNING:root:it is a warning
ERROR:root:I think file not found
CRITICAL:root:Internet is not working

logging.WARNING

#importing module 
import logging 
logging.basicConfig(level=logging.WARNING) 
#Test messages 
logging.debug("its is safe") #This will not print
logging.info("it is a information") #This will not print
logging.warning("it is a warning") 
logging.error("I think file not found") 
logging.critical("Internet is not working")

Output

C:\Users\ASUS\Desktop>python cmp.py
WARNING:root:it is a warning
ERROR:root:I think file not found
CRITICAL:root:Internet is not working

logging.ERROR

#importing module 
import logging 
logging.basicConfig(level=logging.ERROR) 
#Test messages 
logging.debug("its is safe") #This will not print
logging.info("it is a information") #This will not print
logging.warning("it is a warning") #This will not print
logging.error("I think file not found") 
logging.critical("Internet is not working")

logging.CRITICAL

#importing module 
import logging 
logging.basicConfig(level=logging.CRITICAL) 
#Test messages 
logging.debug("its is safe") #This will not print
logging.info("it is a information") #This will not print
logging.warning("it is a warning") #This will not print
logging.error("I think file not found") #This will not print
logging.critical("Internet is not working")

Output

C:\Users\ASUS\Desktop>python cmp.py
CRITICAL:root:Internet is not working

Note All the level is set according to the numeric number of logging.

Subscribe Now