Skip to content

Instantly share code, notes, and snippets.

@KabakiAntony
Created May 9, 2023 05:20
Show Gist options
  • Save KabakiAntony/ff1891b9d1ed741c283c9b6a3c6710a2 to your computer and use it in GitHub Desktop.
Save KabakiAntony/ff1891b9d1ed741c283c9b6a3c6710a2 to your computer and use it in GitHub Desktop.
an example showing some of the basic concepts in logging in python
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