Automatic exception logging for Django applications.
Project description
Django Rest Exception Logger
This package provides middleware for automatically logging exceptions in Django applications. It captures and stores exception details such as the error message, stack trace, request data, headers, and the view where the error occurred.
Features
- Automatically logs exceptions in the database
- Captures request payloads and headers for debugging
- Customizable via model relationships with your user model
- Handles specific exceptions like
PermissionDeniedandParseErrorwith customized responses
Requirements
- Python 3.8+
- Django 3.2+
- Django Rest Framework 3.12+
Installation
-
Install the package
You can install this package from PyPI using pip:
pip install django-rest-exception-logger
-
Add the middleware
To enable automatic exception logging, add
ExceptionMiddlewareto yourMIDDLEWARElist in your Django project'ssettings.pyfile:MIDDLEWARE = [ # Other middlewares... 'django_rest_exception_logger.middleware.ExceptionMiddleware', ]
-
Add the ExceptionLog model
This package comes with the
ExceptionLogmodel, which stores information about exceptions. You need to integrate it into your project.-
Add
exception_loggingto theINSTALLED_APPSlist insettings.py:INSTALLED_APPS = [ # Other apps... 'django_rest_exception_logger', ]
-
Run migrations to create the necessary database tables:
python manage.py migrate
-
if migration not detected, run this command
python manage.py makemigrations django_rest_exception_logger
-
-
Configure the user model (optional)
By default, the
ExceptionLogmodel has a foreign key to theauth.Usermodel. If you are using a custom user model, ensure that theExceptionLogmodel'suserfield is compatible with your custom user model. -
Customize Middleware behavior (optional)
You can extend the
ExceptionMiddlewareor modify it to add additional custom exception handling as needed for your application.
Usage
Once the middleware is installed, it will automatically log exceptions that occur in your views to the ExceptionLog model. The logged information will include:
- Exception type and message
- Full traceback
- Request payload (for POST, PUT, PATCH requests)
- Request headers and parameters
- View name where the exception occurred
- User who initiated the request (if available)
Usage Examples
# 1. Query All Error Logs
from django_rest_exception_logger.models import ExceptionLog
# Retrieve all error logs ordered by most recent
error_logs = ExceptionLog.objects.filter(log_type="error").order_by('-timestamp')
for log in error_logs:
print(log.message, log.timestamp)
# 2. Create a New Info Log
from django_rest_exception_logger.models import ExceptionLog
# Create a new info log entry
ExceptionLog.objects.create(
log_type="info",
message="This is an informational log",
view_name="my_view"
)
Example Use Cases
- Debugging Production Issues: Track unexpected errors in production environments with full context, including which user triggered the error and what data they sent.
- Security Auditing: Capture unauthorized access attempts and permission errors for analysis and potential remediation.
- Improving User Experience: With detailed logs, identify common errors and enhance your application's stability and error handling.
Testing
To test the functionality of the middleware, you can manually raise an exception in one of your views and check the logs in your admin or database:
def test_view(request):
raise Exception("Test exception for logging")
After visiting this view, a new entry should appear in the ExceptionLog model, capturing the details of the error.
Contributing
Feel free to open an issue or submit a pull request if you have suggestions for improvements.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-rest-exception-logger-0.1.4.tar.gz.
File metadata
- Download URL: django-rest-exception-logger-0.1.4.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3450d4318eae552d8c7cef7e097b178187001e73ce7cc609dee2997783788e34
|
|
| MD5 |
13f914872f6aef3d80b213e55dd2d5f1
|
|
| BLAKE2b-256 |
90e88e56fda5835da9ac818e2a24b6e5abe1b54653efc406c466e801ce2ea154
|
File details
Details for the file django_rest_exception_logger-0.1.4-py3-none-any.whl.
File metadata
- Download URL: django_rest_exception_logger-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
874fd322c297c052400450f45e4a94bbbec9cb56a036c8b15cb8377e2b39c104
|
|
| MD5 |
72825626ae05ebecb80edeff3b2f4ec9
|
|
| BLAKE2b-256 |
dd60fb6a56c19a8fa8051f4cc26756d885004510299c6233ffcca87e03dd495b
|