πŸ”₯ Welcome to my blog! Latest updates available now. Visit daily! πŸ”₯ πŸ”₯ Click Here for Latest Updates – Visit My Blog πŸ”₯
πŸ”₯ Latest Updates πŸ“Š GP Grade Finder πŸ“’ Government Services Info πŸ†• New Schemes Updates

How to implement logging in Python

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

Quatation of the Day 22-03-2026