Log unhandled exceptions as critical
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file unhandled_exception_logger-0.0.3.tar.gz
.
File metadata
- Download URL: unhandled_exception_logger-0.0.3.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2e427c17c3565ab8e9a87a342e1551595c167ee68a6bca16aed9ffb24a91065 |
|
MD5 | 32c4220670f29e1282c9f58d1707fc9f |
|
BLAKE2b-256 | ef7b3798265b1694cebedf2348b6aec005527388aae779cf3f3a44c3329e3cec |
File details
Details for the file unhandled_exception_logger-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: unhandled_exception_logger-0.0.3-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c3f098cf3b9dc6c0f83de20b545fe8267b8094c9d39c67527f734aa911edac0 |
|
MD5 | 5de8fcb763ec4d6879a407d14669c466 |
|
BLAKE2b-256 | 5f2da78192726bff09d5d03a26e624bb3340bc724af26734a4620d5d2ea6e810 |