Skip to main content

Middleware that makes a request GUID available from anywhere and injects it into your logs.

Project description

GUID injection for Django

https://img.shields.io/pypi/v/django-guid.svg https://img.shields.io/pypi/pyversions/django-guid.svg https://img.shields.io/pypi/djversions/django-guid.svg

Django GUID attaches a GUID to the local thread of a request. The GUID is accessible from anywhere within the application throughout a request, making it possible to inject the GUID into the logs.

Installation

Python package:

pip install django-guid

In your project’s settings.py add these settings:

Add the middleware to the middleware (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:

'handlers': {
    'console': {
        'class': 'logging.StreamHandler',
        'formatter': 'medium',
        'filters': ['correlation_id'],
    }
}

and lastly make sure we add it to the format:

'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.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-guid-0.2.1.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

django_guid-0.2.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page