Skip to main content

Middleware to log http requests to postgresql

Project description

Django-Requestlog

Quality Gate Status Coverage

Middleware who logs each request with their headers and the body into to PostgreSQL for diagnostic purposes.

Why Logging to the database?

Logging to the database instead of to a logfile has the advantage that it can be searched by people not having access to the server logs. Of course this only works for low traffic sites. And I recommend to purge those logs regularly with the provided manage command or celery task.

What is logged?

The log is written to the table requestlog_requestlog.

  • timestamp
  • client IP address
  • django user-id
  • method
  • URL
  • header fields
  • cookies
  • query parameter
  • POST body
  • HTTP status code
  • the first 1024 of the response

Credits

Based on https://github.com/ASKBOT/django-request-logging

Settings

Request logging has to be explicitely enabled (for performance reasons):

REQUEST_LOGGING_ENABLED = True

You can ask the service to ignore certain fields from the querystring and the header. You do this by setting the name of the fields in the Django settings variable REQUEST_LOGGING_HIDE_PARAMETERS. For example like this:

REQUEST_LOGGING_HIDE_PARAMETERS = [ 'HTTP_AUTHORIZATION', 'password' ]

Filter certain IPs (e.g. internal Kubernetes health check requests)

REQUEST_LOGGING_IGNORE_IPS = [ '10.2.3.4' ]

Filter certain paths by Regex (including query string):

REQUEST_LOGGING_IGNORE_PATHS = [ '/admin/login' ]

Purge old entries

You can use the following management command which deletes entries older than 3 days:

./manage.py requestlog_purge

Or you trigger the task with Celery beat by adding a shared task to your projects task list:

In your settings:

CELERY_BEAT_SCHEDULE = {
    ...    
    'requestlog.tasks.delete_old_requestlog_entries': {
        'task': 'requestlog.tasks.delete_old_requestlog_entries',
        'schedule': crontab(hour=2, minute=0)
        'args': (30,) # This is the number of days to keep the entries 
    },
}

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-requestlog-1.1.8.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

django_requestlog-1.1.8-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file django-requestlog-1.1.8.tar.gz.

File metadata

  • Download URL: django-requestlog-1.1.8.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for django-requestlog-1.1.8.tar.gz
Algorithm Hash digest
SHA256 93250a3be8392c9a7c222fae59aa541fb38efc056ac469a40647f44b9f6304cd
MD5 c59b1650da720fa82c2a0e3b023a3b1d
BLAKE2b-256 eeca5fb56e5f3fdda2bc32f16d108ca8c4a279cba62a85f0da292fff99b63ac2

See more details on using hashes here.

File details

Details for the file django_requestlog-1.1.8-py3-none-any.whl.

File metadata

  • Download URL: django_requestlog-1.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for django_requestlog-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 21e6606b852138e2cd8766bc14916798bbadca39affa5f417fcbaa89b2488ac6
MD5 ac4b27d76efa5ef590ef30e737741ab7
BLAKE2b-256 0d811ced4de474146fe2dfaa1065f1c921d63238d82e9d21d16d8be9407b7f9d

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