Skip to content

Instantly share code, notes, and snippets.

@rajvermacas
Created October 28, 2024 06:45
Show Gist options
  • Save rajvermacas/a2aed889f9effa4391b20923bb9c8173 to your computer and use it in GitHub Desktop.
Save rajvermacas/a2aed889f9effa4391b20923bb9c8173 to your computer and use it in GitHub Desktop.
python_logger
import logging
# Create a custom logger for your project
my_logger = logging.getLogger('my_project')
my_logger.setLevel(logging.DEBUG)
# Create a handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
# Create a formatter and add it to the handler
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
# Add the handler to the logger
my_logger.addHandler(console_handler)
# External libraries will not flood the log
logging.getLogger('psparser').setLevel(logging.WARNING)
# Log messages with your custom logger
my_logger.debug("Debug message from my_project")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment