Skip to main content

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

Metadata

Release files for drel 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drel 0.5.0
File Size Uploaded Provenance Actions
drel-0.5.0.tar.gz 8.3 kB Without provenance Details for drel-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drel 0.5.0
File Interpreter ABI Platform Actions
drel-0.5.0-py3-none-any.whl Python 3 none any Details for drel-0.5.0-py3-none-any.whl

Total release size: 22.4 kB

Release files / drel-0.5.0.tar.gz

Download URL drel-0.5.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
27cf7dbe2bfc8a98cd57b6acf495ab425fb9dbe341cd6dc6adc1eb81f0688602
BLAKE2b-256 checksum
How to use checksums
a4ca5883c48ded3d5d5c2896d9ce6e645606bb9128f66b6dc547c866a59f3b5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

Release files / drel-0.5.0-py3-none-any.whl

Download URL drel-0.5.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c3bea507abd58ea777c83f4f0d012f75245fe295eca193479fe5fc01f78d57b
BLAKE2b-256 checksum
How to use checksums
72ea218cda7b2253e743c29d1892e8b8f6e8df958a4efe5f730bd60c416dc8bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

Security for drel 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page