To perform logging, first we are required to create a file to store messages and we have to specify which level messages we have to store.
We can do this by using basicConfig() function of logging module.
Copy code
Python
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.
Copy code
Python
logging.debug(message)
logging.info(message)
logging.warning(message)
logging.error(message)
logging.critical(message)
Q. Write a Python program to create a log file and write WARNING and higher level messages?
Copy code
Python
1) import logging
2) logging.basicConfig(filename='log.txt', level=logging.WARNING)
3) print("Logging Module Demo")
4) logging.debug("This is debug message")
5) logging.info("This is info message")
6) logging.warning("This is warning message")
7) logging.error("This is error message")
8) logging.critical("This is critical message")
log.txt:
Copy code
1) WARNING:root:This is warning message
2) ERROR:root:This is error message
3) CRITICAL:root:This is critical message
No comments:
Post a Comment
Thank you Very Much.For Given Comment