Skip to main content

Log outgoing requests made by the requests python library

Project description

1 django-log-outgoing-requests

Version:

0.1.0

Source:

https://github.com/maykinmedia/django-log-outgoing-requests

Keywords:

logging

PythonVersion:

3.9

Build status Code quality checks black Coverage status Documentation Status

python-versions django-versions pypi-version

Log and save outgoing requests

The current library logs only the requests made by the requests library

2 Features

  • Log outgoing requests made by requests library

  • Save logs in database

  • Overview of the saved logs in the admin page

3 Installation

3.1 Requirements

  • Python 3.7 or above

  • setuptools 30.3.0 or above

  • Django 3.2 or newer

  • requests

3.2 Install

pip install django-log-outgoing-requests

4 Usage

To use this with your project you need to follow these steps:

  1. Add Django Log Outgoing Requests to INSTALLED_APPS in your Django project’s settings.py:

    INSTALLED_APPS = (
      # ...,
      "log_outgoing_requests"
    )
  2. Update your settings.py file with the following (if you haven’t defined logging yet, you can see the Django’s documentation):

    from log_outgoing_requests.formatters import HttpFormatter
    
    
    LOGGING = {
        #...,
        "formatters": {
            #...,
            "outgoing_requests": {"()": HttpFormatter},
        },
        "handlers": {
            #...,
            "log_outgoing_requests": {
                "level": "DEBUG",
                "formatter": "outgoing_requests",
                "class": "logging.StreamHandler",
            },
            "save_outgoing_requests": {
                "level": "DEBUG",
                "class": "log_outgoing_requests.handlers.DatabaseOutgoingRequestsHandler",
            },
        },
        "loggers": {
            #...,
            "requests": {
                "handlers": ["log_outgoing_requests", "save_outgoing_requests"],
                "level": "DEBUG",
                "propagate": True,
            },
        },
    }
    
    LOG_OUTGOING_REQUESTS_DB_SAVE = True # save logs enabled/disabled based on the boolean value
    LOG_OUTGOING_REQUESTS_DB_SAVE_BODY = True # save request/response body
    LOG_OUTGOING_REQUESTS_EMIT_BODY = True # log request/response body
    LOG_OUTGOING_REQUESTS_CONTENT_TYPES = [
            "text/*",
            "application/json",
            "application/xml",
            "application/soap+xml",
    ] # save request/response bodies with matching content type
    LOG_OUTGOING_REQUESTS_MAX_CONTENT_LENGTH = 524_288  # maximal size (in bytes) for the request/response body
    LOG_OUTGOING_REQUESTS_LOG_BODY_TO_STDOUT = True
  3. Run the migrations

    python manage.py migrate
  4. Make some requests using requests library within the Django context, for example using python manage.py shell

    import requests
    res = requests.get("https://httpbin.org/json")
    print(res.json())
  5. Check stdout for the printable output, and navigate to Admin > Miscellaneous > Outgoing Requests Logs to see the saved log records. In order to override the settings for saving logs, navigate to Admin > Miscellaneous > Outgoing Requests Log Configuration.

5 Local development

To install and develop the library locally, use:

pip install -e --no-build-isolation .[tests,coverage,docs,pep8,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-log-outgoing-requests-0.2.0.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-log-outgoing-requests-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django-log-outgoing-requests-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d954330f3e137fde39c9d24e92a84e21ec0c14fb5722fb92b37b5d5465e17072
MD5 f14abf111301a792fcaa1d57555d6283
BLAKE2b-256 88a02a1cb31f3c723ebd1952480aafc9d5b713869abee25c96f1636e250beadf

See more details on using hashes here.

File details

Details for the file django_log_outgoing_requests-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_log_outgoing_requests-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6dcfe1fb0cdc0f6b2f9b44fda153df54922da104da54b7499757f9e69dac3633
MD5 99abadf73cda8b60b39c9352c0306ac8
BLAKE2b-256 4ac545cbd420cab81c4be39c0175c77d471f0db54c894cd5bdc1ddb1fbab5388

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