- It is highly recommended to store complete application flow and exceptions information to a file.
- This process is called logging.
- The main advanatages of logging are:
- We can use log files while performing debugging
- We can provide statistics like number of requests per day etc.
- To implement logging, Python provides inbuilt module logging.
Logging Levels
- Depending on type of information, logging data is divided according to the following 6 levels in python
CRITICAL===>50
- Represents a very serious problem that needs high attention
ERROR ===>40
- Represents a serious error
WARNING ==>30
- Represents a warning message, some caution needed. It is alert to the programmer.
INFO==>20
- Represents a message with some important information
DEBUG ===>10
- Represents a message with debugging information
NOTSET==>0
- Represents that level is not set
- By default while executing Python program only WARNING and higher level messages will be displayed.
Implement Logging
- To perform logging, first we required to create a file to store messages and we have to specify
- which level messages required to store.
- We can do this by using basicConfig() function of logging module.
- logging.basicConfig(filename=’log.txt’,level=logging.WARNING)
- The above line will create a file log.txt and we can store either WARNING level or higher level
- messages to that file.
- After creating log file, we can write messages to that file by using the following methods
- logging.debug(message)
- logging.info(message)
- logging.warning(message)
- logging.error(message)
- logging.critical(message)
Question
Write a Python Program to create a loge file and write WARNING and Higher level messages?
Python
import logging
logging.basicConfig(filename='log.txt',level=logging.WARNING)
print('Logging Demo')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
Output
PowerShell
WARNING:root:warning Information
ERROR:root:error Information
CRITICAL:root:critical Information
Note
- In the above program only WARNING and higher level messages will be written to the log file. If we set level as DEBUG then all messages will be written to the log file.
test.py
Python
import logging
logging.basicConfig(filename='log.txt',level=logging.DEBUG)
print('Logging Demo')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
Output
PowerShell
DEBUG:root:Debug Information
INFO:root:info Information
WARNING:root:warning Information
ERROR:root:error Information
CRITICAL:root:critical Information
How to configure log file in over writing mode
- In the above program by default data will be appended to the log file.i.e append is the default mode. Instead of appending if we want to over write data then we have to use filemode property.
- logging.basicConfig(filename=’log786.txt’,level=logging.WARNING)
- meant for appending
- logging.basicConfig(filename=’log786.txt’,level=logging.WARNING,filemode=’a’)
- explicitly we are specifying appending.
- logging.basicConfig(filename=’log786.txt’,level=logging.WARNING,filemode=’w’)
- meant for over writing of previous data.
Note
- logging.basicConfig(filename=’log.txt’,level=logging.DEBUG)
- If we are not specifying level then the default level is WARNING(30)
- If we are not specifying file name then the messages will be printed to the console.
test.py
Python
import logging
logging.basicConfig()
print('Logging Demo')
logging.debug('Debug Information')
logging.info('info Information')
logging.warning('warning Information')
logging.error('error Information')
logging.critical('critical Information')
Output
PowerShell
D:\durgaclasses>py test.py
Logging Demo
WARNING:root:warning Information
ERROR:root:error Information
CRITICAL:root:critical Information