Middleware that makes a request GUID available from anywhere and injects it into your logs.
Project description
Django GUID
Django GUID stores a GUID to an object, making it accessible by using the ID of the current thread. The GUID is accessible from anywhere within the application throughout a request, allowing us to inject it into the logs.
Free software: BSD License
Homepage: https://github.com/JonasKs/django-guid
Documentation: https://django-guid.readthedocs.io
Settings
SKIP_CLEANUP
After the request is done, the GUID is deleted to avoid memory leaks. Memory leaks can happen in the case of many threads, or especially when using Gunicorn
max_requests
or similar settings, where the number of thread IDs can potentially scale for ever. Having clean up enabled ensures we can not have memory leaks, but comes at the cost that anything that happens after this middleware will not have the GUID attached, such asdjango.request
ordjango.server
logs. If you do not want clean up of GUIDs and know what you’re doing, you can enableSKIP_CLEANUP
.Default: False
GUID_HEADER_NAME
The name of the GUID to look for in a header in an incoming request. Remember that it’s case insensitive.
Default: Correlation-ID
VALIDATE_GUID
Whether the
GUID_HEADER_NAME
should be validated or not. If the GUID sent to through the header is not a valid GUID (uuid.uuid4
).Default: True
Installation
Python package:
pip install django-guid
In your project’s settings.py
add these settings:
(If these settings are confusing, please have a look in the demo project settings.py file for a complete setup.)
Add the middleware to the MIDDLEWARE
setting (To ensure the GUID to be injected in all logs, put it on top):
MIDDLEWARE = [
'django_guid.middleware.GuidMiddleware',
...
]
Add a filter to your LOGGING:
LOGGING = {
'filters': {
'correlation_id': {
'()': 'django_guid.log_filters.CorrelationId'
}
}
}
and put that filter in your handler:
LOGGING = {
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'medium',
'filters': ['correlation_id'],
}
}
}
and lastly make sure we add the new correlation_id filter to the formatters:
LOGGING = {
'formatters': {
'medium': {
'format': '%(levelname)s %(asctime)s [%(correlation_id)s] %(name)s %(message)s'
}
}
}
Inspired by django-log-request-id with a django-crequest approach.
Changelog
0.3.1 - 2020-01-13
Improvements
Changed logging from f’strings’ to %strings.
0.3.0 - 2020-01-10
Features
Added a SKIP_CLEANUP setting
Improvements
Improved all tests to be more verbose
Improved the README with more information and a list of all the available settings.
0.2.3 - 2020-01-09
Improvements
Added tests written in pytests, 100% codecov
Added Django2.2 and Django3 to github workflow as two steps
Improved logging
0.2.2 - 2019-12-21
Improvements
Removed the mandatory DJANGO_GUID settings in settings.py. Added an example project to demonstrate how to set the project up
0.2.1 - 2019-12-21
Improvements
Workflow added, better docstrings, easier to read flow
0.2.0 - 2019-12-21
Features
Header name and header GUID validation can be specified through Django settings
2019-12-20
Initial release
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
Hashes for django_guid-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05084f0530c2b3a88e8646bc185904216ded213daae1f7596b8053b8a1117aab |
|
MD5 | 9a6dbfb391293c7566a54d0893d62281 |
|
BLAKE2b-256 | ee5de15436f939ec980c3b89de2318618802bd828fa8abeac250c471e2c6eb7c |