Skip to main content

Augment each request with unique id for logging purposes

Project description

Latest Version Downloads License

Augments each request with unique request_id attribute and provides request id logging helpers.

Developed and used at en.ig.ma software shop.

Quickstart

  1. Include django-request-id in your requirements.txt file.

  2. Add request_id to INSTALLED_APPS (necessary only if you are going to use the {% request_id %} template tag).

  3. Add request_id.middleware.RequestIdMiddleware to the top of MIDDLEWARE_CLASSES.

  4. The app integrates with the standard Python/Django logging by defining a filter that puts a request_id variable in scope of every log message.

    First add a filter definition to the Django LOGGING settings:

    "filters": {
        "request_id": {
            "()": "request_id.logging.RequestIdFilter"
        }
    }

    Then enable the filter for related handlers:

    "handlers": {
        "console": {
            ...
            "filters": ["request_id"],
        }
     }

    And finally modify formatter output format to include the %(request_id) placeholder:

    "formatters": {
        "console": {
            "format": "%(asctime)s - %(levelname)-5s [%(name)s] request_id=%(request_id)s %(message)s"
        }
    }

    A full Django logging config example may look like this:

    LOGGING= {
        "version": 1,
        "disable_existing_loggers": False,
        "filters": {
            "request_id": {
                "()": "request_id.logging.RequestIdFilter"
            }
        },
        "formatters": {
            "console": {
                "format": "%(asctime)s - %(levelname)-5s [%(name)s] request_id=%(request_id)s %(message)s",
                "datefmt": "%H:%M:%S"
            }
        },
        "handlers": {
            "console": {
                "level": "DEBUG",
                "filters": ["request_id"],
                "class": "logging.StreamHandler",
                "formatter": "console"
            }
        },
        "loggers": {
            "": {
                "level": "DEBUG",
                "handlers": ["console"]
            }
        }
    }
  5. Make sure that your web server adds a X-Request-ID header to each request (and logs it in the server log for further matching of the server and app log entries).

    Heroku handles this automatically. On Nginx you may require a separate module (see nginx_requestid or nginx-x-rid-header). On Apache you need to a2enmod the unique_id module and set REQUEST_ID_HEADER = "UNIQUE_ID" in the Django project settings.

    If you can’t generate the X-Request-Id header at the web server level then simply set REQUEST_ID_HEADER = None in your project settings and the app will generate a unique id value automatically instead of retrieving it from the wsgi environment.

    For more info on server configs see server-config.

Dependencies

django-request-id depends on django-appconf>=0.6.

Documentation

The full documentation is at http://django-request-id.rtfd.org.

There’s also an instant demo example that can be run from the cloned repository:

python demo.py

License

django-request-id is released under the MIT license.

Other Resources

Commercial Support

This app and many other help us build better software and focus on delivering quality projects faster. We would love to help you with your next project so get in touch by dropping an email at en@ig.ma.

History

0.1.0 (2014-01-30)

  • First 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-request-id-0.1.0.zip (14.6 kB view details)

Uploaded Source

Built Distribution

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

django_request_id-0.1.0-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-request-id-0.1.0.zip.

File metadata

File hashes

Hashes for django-request-id-0.1.0.zip
Algorithm Hash digest
SHA256 af87929d7ec1960b74cd471b0f35831447a9c73ee4fe2628ae30037458143d39
MD5 38bce7dee965d3a5b307f66f36c6aa3d
BLAKE2b-256 0d28ce1a947d5a79fb5fe9f74d17118921acc33080299f8a4021582edfb1ec54

See more details on using hashes here.

File details

Details for the file django_request_id-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_request_id-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d7b18bde1e190ec1292e276c16403f95881b81c949114481dc902f1ee0766705
MD5 f74344a4eacbd55ad7577b03436b0129
BLAKE2b-256 5955dc9c489e4ce57589c61dbbf52ddcb9045630fa348c48854c85b199c7482d

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