Skip to main content

Django middleware and log filter to attach a unique ID to every log message generated as part of a request

Project description

nhst-log-request-id

Django middleware and log filter to attach a unique ID to every log message generated as part of a request.

Author: This repository forked and updated form here github written by Jamie Matthews, @j4mie


DEBUG [33031a43fc244539895fef70c433337e] myproject.apps.myapp.views: Doing something in a view
DEBUG [33031a43fc244539895fef70c433337e] myproject.apps.myapp.forms: The form validated successfully!
DEBUG [33031a43fc244539895fef70c433337e] myproject.apps.myapp.models: Doing some model magic
DEBUG [33031a43fc244539895fef70c433337e] myproject.apps.myapp.views: Redirecting to form success page

How?

The request ID is stored in a thread local. Use of thread locals is not generally considered best practice for Django applications, but seems to be the only viable approach in this case. Pull requests with better ideas are welcome.

Installation and usage

First, install the package: pip install nhst-log-request-id

Add the middleware to your MIDDLEWARE_CLASSES setting. It should be at the very top.

MIDDLEWARE_CLASSES = (
    'nhst_log_request_id.middleware.RequestIDMiddleware',
    # ... other middleware goes here
)

Add the nhst_log_request_id.filters.RequestIDFilter to your LOGGING setting. You'll also need to update your formatters to include a format with the new request_id variable, add a handler to output the messages (eg to the console), and finally attach the handler to your application's logger.

If none of the above made sense, study Django's logging documentation.

An example LOGGING setting is below:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'request_id': {
            '()': 'nhst_log_request_id.filters.RequestIDFilter'
        }
    },
    'formatters': {
        'standard': {
            'format': '%(levelname)-8s [%(asctime)s] [%(correlationId)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'filters': ['request_id'],
            'formatter': 'standard',
        },
    },
    'loggers': {
        'myapp': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

And following settings for consistant log messages across other projects

LOG_REQUEST_ID_HEADER = 'HTTP_X_CORRELATION_ID'
GENERATE_REQUEST_ID_IF_NOT_IN_HEADER = True
REQUEST_ID_RESPONSE_HEADER = 'X-Correlation-Id'
REQUEST_ID_PROPERTY_NAME = 'correlationId'

You can then output log messages as usual:

import logging
logger = logging.getLogger(__name__)
logger.debug("A wild log message appears!")

To check json object of final log which will be sync with ELK Stack in local, add following line in you local.py setting

LOGGING["handlers"]["console"]['formatter'] = "json"

And in browser, you can see X-Correlation-Id: in response header of any request which coming through configured pipeline.

Ougoing request from django app

To include correlationId in ougoing request from configured django app to other app, we can access current correlationId from local object of nhst_log_request_id package. You can modify request header of outgoing request in a centralize place.

import this in you code:

from nhst_log_request_id import DEFAULT_REQUEST_ID_PROPERTY_NAME, REQUEST_ID_PROPERTY_NAME_SETTING, REQUEST_ID_RESPONSE_HEADER_SETTING, local

and modify request before sending request:

if(local):
    header_property = getattr(settings, REQUEST_ID_RESPONSE_HEADER_SETTING)
    request_id_property_name = getattr(settings, REQUEST_ID_PROPERTY_NAME_SETTING, DEFAULT_REQUEST_ID_PROPERTY_NAME)
    self.headers[header_property] = local.__dict__.get(request_id_property_name,'none')

License

Copyright © 2012-2018, DabApps.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

nhst-log-request-id-1.0.2.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nhst_log_request_id-1.0.2-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file nhst-log-request-id-1.0.2.tar.gz.

File metadata

  • Download URL: nhst-log-request-id-1.0.2.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for nhst-log-request-id-1.0.2.tar.gz
Algorithm Hash digest
SHA256 3c6c3db7d85f9ae16bcf685cd70dd80f3ec58d0d0372ab3c873138e2c6ebee15
MD5 0449a74d0036b9f1f751fa0f8e21499c
BLAKE2b-256 32ea2f24801a6d7fbb52daf992c42bc2ebb9bcf0703fb9f56a124ee0b054cecc

See more details on using hashes here.

File details

Details for the file nhst_log_request_id-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: nhst_log_request_id-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for nhst_log_request_id-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5ef7f8dc4fef5bfc5eb6da990b2ddaf5dc428f68320c102820db5b744773da5
MD5 e07b01de6807b7dbeae876ad9d9bff7d
BLAKE2b-256 17a6353497562c07e0731fc8bf17b769fbeefc111ba3b11ec7381198e968ca02

See more details on using hashes here.

Supported by

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