Package: django-riso-guid
Django GUIDDjango GUID attaches a unique correlation ID/request ID to all your log outputs for every request.
In other words, all logs connected to a request now has a unique ID attached to it, making debugging simple.Which version of Django GUID you should use depends on your Django version and whether you runASGIorWSGIservers.
To determine which Django-GUID version you should use, please see the table below.Django versionDjango-GUID version3.1.1 or above3.x.x - ASGI and WSGI3.0.0 - 3.1.02.x.x - Only WSGI2.2.x2.x.x - Only WSGIDjango GUID >= 3.0.0 usesContextVarto store and access the GUID. Previous versions stored the GUID to an object,
making it accessible by using the ID of the current thread. (Version 2 of Django GUID is supported until Django2.2 LTS has passed.)Resources:Free software: BSD LicenseDocumentation:https://django-guid.readthedocs.ioHomepage:https://github.com/snok/django-guidExamplesLog output with a GUID:INFO ... [773fa6885e03493498077a273d1b7f2d] project.views This is a DRF view log, and should have a GUID.
WARNING ... [773fa6885e03493498077a273d1b7f2d] project.services.file Some warning in a function
INFO ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.views This is a DRF view log, and should have a GUID.
INFO ... [99d44111e9174c5a9494275aa7f28858] project.views This is a DRF view log, and should have a GUID.
WARNING ... [0d1c3919e46e4cd2b2f4ac9a187a8ea1] project.services.file Some warning in a function
WARNING ... [99d44111e9174c5a9494275aa7f28858] project.services.file Some warning in a functionLog output without a GUID:INFO ... project.views This is a DRF view log, and should have a GUID.
WARNING ... project.services.file Some warning in a function
INFO ... project.views This is a DRF view log, and should have a GUID.
INFO ... project.views This is a DRF view log, and should have a GUID.
WARNING ... project.services.file Some warning in a function
WARNING ... project.services.file Some warning in a functionSee thedocumentationfor more examples.InstallationInstall using pip:pipinstalldjango-guidSettingsPackage settings are added in yoursettings.py:DJANGO_GUID={'GUID_HEADER_NAME':'Correlation-ID','VALIDATE_GUID':True,'RETURN_HEADER':True,'EXPOSE_HEADER':True,'INTEGRATIONS':[],'IGNORE_URLS':[],'UUID_LENGTH':32,}Optional ParametersGUID_HEADER_NAMEThe name of the GUID to look for in a header in an incoming request. Remember that it’s case insensitive.Default: Correlation-IDVALIDATE_GUIDWhether theGUID_HEADER_NAMEshould be validated or not.
If the GUID sent to through the header is not a valid GUID (uuid.uuid4).Default: TrueRETURN_HEADERWhether to return the GUID (Correlation-ID) as a header in the response or not.
It will have the same name as theGUID_HEADER_NAMEsetting.Default: TrueEXPOSE_HEADERWhether to returnAccess-Control-Expose-Headersfor the GUID header ifRETURN_HEADERisTrue, has no effect ifRETURN_HEADERisFalse.
This is allows the JavaScript Fetch API to access the header when CORS is enabled.Default: TrueINTEGRATIONSWhether to enable any custom or available integrations withdjango_guid.
As an example, usingSentryIntegration()as an integration would set Sentry’stransaction_idto
match the GUID used by the middleware.Default: []IGNORE_URLSURL endpoints where the middleware will be disabled. You can put your health check endpoints here.Default: []UUID_LENGTHLets you optionally trim the length of the package generated UUIDs.Default: 32ConfigurationOnce settings have set up, add the following to your projects’settings.py:1. Installed AppsAdddjango_guidto yourINSTALLED_APPS:INSTALLED_APPS=[...'django_guid',]2. MiddlewareAdd thedjango_guid.middleware.guid_middlewareto yourMIDDLEWARE:MIDDLEWARE=['django_guid.middleware.guid_middleware',...]It is recommended that you add the middleware at the top, so that the remaining middleware loggers include the requests GUID.3. Logging ConfigurationAdddjango_guid.log_filters.CorrelationIdas a filter in yourLOGGINGconfiguration:LOGGING={...'filters':{'correlation_id':{'()':'django_guid.log_filters.CorrelationId'}}}Put that filter in your handler:LOGGING={...'handlers':{'console':{'class':'logging.StreamHandler','formatter':'medium','filters':['correlation_id'],}}}And make sure to add the newcorrelation_idfilter to one or all of your formatters:LOGGING={...'formatters':{'medium':{'format':'%(levelname)s%(asctime)s[%(correlation_id)s]%(name)s%(message)s'}}}If these settings were confusing, please have a look in the demo projects’settings.pyfile for a complete example.4. Django GUID Logger (Optional)If you wish to see the Django GUID middleware outputs, you may configure a logger for the module.
Simply add django_guid to your loggers in the project, like in the example below:LOGGING={...'loggers':{'django_guid':{'handlers':['console','logstash'],'level':'WARNING','propagate':False,}}}This is especially useful when implementing the package, if you plan to pass existing GUIDs to the middleware, as misconfigured GUIDs will not raise exceptions, but will generate warning logs.