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 hashes)

Uploaded Source

Built Distribution

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