Created
May 9, 2023 05:20
-
-
Save KabakiAntony/ff1891b9d1ed741c283c9b6a3c6710a2 to your computer and use it in GitHub Desktop.
an example showing some of the basic concepts in logging in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
# Create a formatter | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
# Create a file handler and set the formatter | |
file_handler = logging.FileHandler('example.log') | |
file_handler.setFormatter(formatter) | |
# Create a logger and add the file handler | |
logger = logging.getLogger(__name__) | |
logger.addHandler(file_handler) | |
# Set the logging level to INFO | |
logger.setLevel(logging.INFO) | |
# Log some messages with different levels | |
logger.debug('Debug message') | |
logger.info('Info message') | |
logger.warning('Warning message') | |
logger.error('Error message') | |
logger.critical('Critical message') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment