Skip to content

Instantly share code, notes, and snippets.

@cgons
Created June 26, 2025 18:13
Show Gist options
  • Select an option

  • Save cgons/404a4350f3d6b961d31774fe28e8fdd0 to your computer and use it in GitHub Desktop.

Select an option

Save cgons/404a4350f3d6b961d31774fe28e8fdd0 to your computer and use it in GitHub Desktop.
Python Recipe - Log All Exceptions

Python Recipe - Log All Exceptions

import logging
import sys

def handle_unhandled_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        # Let KeyboardInterrupt pass through for normal exits
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return
    logger = logging.getLogger(__name__)
    logger.critical("Unhandled Exception", exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = handle_unhandled_exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment