Skip to main content

Log Unhandled Exceptions

This package will log unhandled exceptions as a critical error.

Install

pip install unhandled-exception-logger

Usage

For use in generic python code:

from unhandled_exception_logger import unhandled_exception_setup

unhandled_exception_setup()

Flask usage

If using Flask, then you'll need to register the handle_exception function using the @app.errorhandler(Exception) decorator, otherwise Flask 500 errors won't be sent.

from unhandled_exception_logger import unhandled_exception_setup, handle_exception
import logging

unhandled_exception_setup()

app = Flask(__name__)
logging.getLogger("werkzeug").disabled = True
app.logger.disabled = True
@app.errorhandler(Exception)
def flask_handle_exception(e):
    handle_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])

@app.route("/")
def home():
    return "home"

If you wish to, you can pass a custom handler to these critical unhandled exceptions:

import sys
import logging
import os
from flask import Flask
from unhandled_exception_logger import (
    unhandled_exception_setup,
    handle_exception,
)

PYTHON_LOG_LEVEL = os.getenv("PYTHON_LOG_LEVEL", "DEBUG")


# Register myChstomHandler log handler
myChstomHandler = MyCustomHandler()
myChstomHandler.setLevel("CRITICAL")

unhandled_exception_setup(handler=myChstomHandler)


logger = logging.getLogger()
logger.setLevel(PYTHON_LOG_LEVEL)
logger.addHandler(myChstomHandler)


# # Direct all uncaught exceptions to handle_exception
sys.excepthook = handle_exception

# Minimal python app example with example unhandled exception
app = Flask(__name__)


@app.errorhandler(Exception)
def flask_handle_exception(e):
    handle_exception(
        sys.exc_info()[0],
        sys.exc_info()[1],
        sys.exc_info()[2],
        handler=MyCustomHandler,  # noqa: E501
    )


@app.errorhandler(500)
def error_page(e):
    return "An error occurred"

@app.route("/")
def home():
    return "home"

@app.route("/error")
def error():
    names = ["Bob", "Alice"]
    print(names[2])
    return "<p>Hello, World!</p>"

Inspired by

Release files for unhandled-exception-logger 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unhandled-exception-logger 0.0.3
File Size Uploaded
unhandled_exception_logger-0.0.3.tar.gz 2.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unhandled-exception-logger 0.0.3
File Interpreter ABI Platform
unhandled_exception_logger-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 5.0 kB

Release files / unhandled_exception_logger-0.0.3.tar.gz

Download URL unhandled_exception_logger-0.0.3.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b2e427c17c3565ab8e9a87a342e1551595c167ee68a6bca16aed9ffb24a91065
BLAKE2b-256 checksum
How to use checksums
ef7b3798265b1694cebedf2348b6aec005527388aae779cf3f3a44c3329e3cec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / unhandled_exception_logger-0.0.3-py3-none-any.whl

Download URL unhandled_exception_logger-0.0.3-py3-none-any.whl
Size 2.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c3f098cf3b9dc6c0f83de20b545fe8267b8094c9d39c67527f734aa911edac0
BLAKE2b-256 checksum
How to use checksums
5f2da78192726bff09d5d03a26e624bb3340bc724af26734a4620d5d2ea6e810
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page