Skip to main content

Django middleware that logs http request body.

Project description

django-request-logging

Plug django-request-logging into your Django project and you will have intuitive and color coded request/response payload logging, for both web requests and API requests. Supports Django 1.8+.

Installing

$ pip install django-request-logging

Then add request_logging.middleware.LoggingMiddleware to your MIDDLEWARE.

For example:

MIDDLEWARE = (
    ...,
    'request_logging.middleware.LoggingMiddleware',
    ...,
)

And configure logging in your app:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['console'],
            'level': 'DEBUG',  # change debug level as appropiate
            'propagate': False,
        },
    },
}

Details

Most of times you don't have to care about these details. But in case you need to dig deep:

  • All logs are configured using logger name "django.request".
  • If HTTP status code is between 400 - 599, URIs are logged at ERROR level, otherwise they are logged at INFO level.
  • If HTTP status code is between 400 - 599, data are logged at ERROR level, otherwise they are logged at DEBUG level.

See REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL setting to override this.

A no_logging decorator is included for views with sensitive data.

By default, value of Http headers HTTP_AUTHORIZATION and HTTP_PROXY_AUTHORIZATION are replaced wih *****. You can use REQUEST_LOGGING_SENSITIVE_HEADERS setting to override this default behaviour with your list of sensitive headers.

Django settings

You can customized some behaves of django-request-logging by following settings in Django settings.py.

REQUEST_LOGGING_DATA_LOG_LEVEL

By default, data will log in DEBUG level, you can change to other valid level (Ex. logging.INFO) if need.

REQUEST_LOGGING_ENABLE_COLORIZE

If you want to log into log file instead of console, you may want to remove ANSI color. You can set REQUEST_LOGGING_ENABLE_COLORIZE=False to disable colorize.

REQUEST_LOGGING_DISABLE_COLORIZE (Deprecated)

This legacy setting will still available, but you should't use this setting anymore. You should use REQUEST_LOGGING_ENABLE_COLORIZE instead. We keep this settings for backward compatibility.

REQUEST_LOGGING_MAX_BODY_LENGTH

By default, max length of a request body and a response content is cut to 50000 characters.

REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL

By default, HTTP status codes between 400 - 499 are logged at ERROR level. You can set REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=logging.WARNING (etc) to override this. If you set REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL=logging.INFO they will be logged the same as normal requests.

REQUEST_LOGGING_SENSITIVE_HEADERS

The value of the headers defined in this settings will be replaced with '*****' to hide the sensitive information while logging. E.g. REQUEST_LOGGING_SENSITIVE_HEADERS = ['HTTP_AUTHORIZATION', 'HTTP_USER_AGENT']

Deploying, Etc.

Maintenance

Use pyenv to maintain a set of virtualenvs for 2.7 and a couple versions of Python 3. Make sure the requirements-dev.txt installs for all of them, at least until we give up on 2.7. At that point, update this README to let users know the last version they can use with 2.7.

Setup

  • pip install twine pypandoc pbr wheel
  • If pypandoc complains that pandoc isn't installed, you can add that via brew if you have Homebrew installed
  • You will need a .pypirc file in your user root folder that looks like this:
    index-servers=
        testpypi
        pypi

    [testpypi]
    username = rhumbix
    password = password for dev@rhumbix.com at Pypi

    [pypi]
    username = rhumbix
    password = password for dev@rhumbix.com at Pypi

Publishing

  • Bump the version value in request_logging/__init__.py
  • Run python setup.py publish
  • Manually tag per the instructions in the output of that command
  • TODO: add automagic git tag logic to the publish process
  • TODO: setup 2FA at Pypi

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-logging-0.7.2.tar.gz (7.8 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_logging-0.7.2-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-request-logging-0.7.2.tar.gz.

File metadata

  • Download URL: django-request-logging-0.7.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for django-request-logging-0.7.2.tar.gz
Algorithm Hash digest
SHA256 5ac99bc885bd30b3b59d3ea8e28ec7fa5d5343748f752cc3ef0c98c1ccf206a8
MD5 fa4bb88a3994bc4f2d3bd9177cbc1a7a
BLAKE2b-256 b348f845315937c5a11a5e0b86afee2c50f1f15f6c96bb093468d497e5a7096e

See more details on using hashes here.

File details

Details for the file django_request_logging-0.7.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_request_logging-0.7.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for django_request_logging-0.7.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c1c6e5fee02a95e4c85e3ebfcc3e0db0b436c8b2bd1f41d645b8e10ee365633
MD5 64cf296e83957537a458ba58cd3527e2
BLAKE2b-256 c6ee6836002e5e622fea224b059492b32245ab0c96d348e44c7ca6671560d289

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