Skip to main content

Django request ElasticSearch logging

Project description

drel

Django request ElasticSearch logging

Django support

To log every django request insert LoggingMiddleware before AuthenticationMiddleware:

# settings.py

MIDDLEWARE = [
    ...,
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "drel.django.LoggingMiddleware",
    ...
]

This will insert request and response info to Elastic Search index called logs-{week_start}-{week_end} in following format:

{
    "timestamp": "2018-12-09 02:22:22",
    "type": "default",
    "request": {
      "url": "https://httpbin.org/post",
      "data": {"param1": "value1"},
      "headers": {}
    },
    "response": {
      "status": 200,
      "data": {"args": {}, "data": "", "files": {}, "form": {"param1": "value1"}, "headers": {"Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "13", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.19.1"}, "json": null, "origin": "130.193.67.76", "url": "https://httpbin.org/post"}
    },
    "app": "default",
    "request_id": "2180930f-859b-4aef-8770-17107fff1170"
}

Requests support

To log requests request and response data use drel.requests.log function:

>>> from drel.requests import post, log
>>> request, response = post("https://httpbin.org/post", {"param1": "value1"})
>>> log(request, response)

Configuration

To change settings override config module values:

from drel import config

config.APPLICATION = "django_app"

Sample Django configuration

from django.http import HttpRequest
from drel.core import config
from drel.django import mail_admins_on_es_exception

def ignore_logging_handler(request: HttpRequest) -> bool:
    return any([
        request.path == "/api/register_device/",
        request.method != "POST",
    ])


config.ELASTIC_SEARCH_EXCEPTION_HANDLER = mail_admins_on_es_exception
config.INDEX_NAME_GETTER = lambda: "django_app_2019-01-01"
config.APPLICATION = "django_app"
config.IGNORE_LOGGING_HANDLER = ignore_logging_handler

This configuration:

  • mail admins on Elastic Search index exception
  • ignore logging non-POST and /api/register_device/ requests
  • insert docs to django_app_2019-01-01 index
  • application field = django_app

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

drel-0.5.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

drel-0.5.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file drel-0.5.1.tar.gz.

File metadata

  • Download URL: drel-0.5.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for drel-0.5.1.tar.gz
Algorithm Hash digest
SHA256 813ad15274fad96a4e123a61c0d46f6322c7ccccf7e11c3f609b2a3bbf6c3d24
MD5 27a2da756fb6b747d9ee11e1ec96330f
BLAKE2b-256 3eb27c3428c785f0f90b08554af29bf9b4a5d455ff95f4c0678d7d24795c242c

See more details on using hashes here.

File details

Details for the file drel-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: drel-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

File hashes

Hashes for drel-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95428c48f1c2e0059773582881b235026ea804c0c02cc29c3f5e43fa50c4b451
MD5 e4075a533f71c2af66af54d1804efccd
BLAKE2b-256 4678e2b62103de3376255cc7001c0db6866bd4e1dbf521badb19fee2cae318c0

See more details on using hashes here.

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