Skip to content

Commit

Permalink
update README and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed May 10, 2023
1 parent 68e8020 commit 5008ad8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ To use this with your project you need to follow these steps:
}
LOG_OUTGOING_REQUESTS_DB_SAVE = True # save logs enabled/disabled based on the boolean value
LOG_OUTGOING_REQUESTS_SAVE_BODY = True # save request/response body
LOG_OUTGOING_REQUESTS_LOG_BODY_TO_STDOUT = True # log request/response body to STDOUT
#. Run the migrations

Expand All @@ -113,7 +116,7 @@ To use this with your project you need to follow these steps:
print(res.json())
#. Check stdout for the printable output, and navigate to ``/admin/log_outgoing_requests/outgoingrequestslog/`` to see
the saved log records
the saved log records. The settings for saving logs can by overridden under ``/admin/log_outgoing_requests/outgoingrequestslogconfig/``.


Local development
Expand Down
3 changes: 3 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Installation
}
LOG_OUTGOING_REQUESTS_DB_SAVE = True # save logs enabled/disabled based on the boolean value
LOG_OUTGOING_REQUESTS_SAVE_BODY = True # save request/response body
LOG_OUTGOING_REQUESTS_LOG_BODY_TO_STDOUT = True # log request/response body to STDOUT
#. Run ``python manage.py migrate`` to create the necessary database tables.

Expand Down
10 changes: 8 additions & 2 deletions log_outgoing_requests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ class Media:

@admin.register(OutgoingRequestsLogConfig)
class OutgoingRequestsLogConfigAdmin(SingletonModelAdmin):
fields = ("save_to_db", "save_body",)
list_display = ("save_to_db", "save_body",)
fields = (
"save_to_db",
"save_body",
)
list_display = (
"save_to_db",
"save_body",
)
17 changes: 11 additions & 6 deletions log_outgoing_requests/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

from django.conf import settings


ALLOWED_CONTENT_TYPES = [
"application/json", "multipart/form-data", "text/html", "text/plain", "", None,
"application/json",
"multipart/form-data",
"text/html",
"text/plain",
"",
None,
]


class DatabaseOutgoingRequestsHandler(logging.Handler):
def emit(self, record):
from .models import OutgoingRequestsLogConfig

config = OutgoingRequestsLogConfig.get_solo()

if config.save_to_db or settings.LOG_OUTGOING_REQUESTS_DB_SAVE:
Expand All @@ -29,8 +34,8 @@ def emit(self, record):
response_content_type = record.res.headers.get("Content-Type", "")

if not (
request_content_type in ALLOWED_CONTENT_TYPES and
response_content_type in ALLOWED_CONTENT_TYPES
request_content_type in ALLOWED_CONTENT_TYPES
and response_content_type in ALLOWED_CONTENT_TYPES
):
return

Expand Down Expand Up @@ -59,8 +64,8 @@ def emit(self, record):
}

if config.save_body or settings.LOG_OUTGOING_REQUESTS_SAVE_BODY:
kwargs["req_body"] = record.req.body,
kwargs["res_body"] = record.res.json(),
kwargs["req_body"] = (record.req.body,)
kwargs["res_body"] = (record.res.json(),)

OutgoingRequestsLog.objects.create(**kwargs)

Expand Down
7 changes: 3 additions & 4 deletions log_outgoing_requests/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from urllib.parse import urlparse

from django.conf import settings
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

from solo.models import SingletonModel

from django.conf import settings


class OutgoingRequestsLog(models.Model):
url = models.URLField(
Expand Down Expand Up @@ -68,7 +67,7 @@ class OutgoingRequestsLog(models.Model):
verbose_name=_("Request body"),
blank=True,
null=True,
help_text=_("The request body.")
help_text=_("The request body."),
)
res_headers = models.TextField(
verbose_name=_("Response headers"),
Expand All @@ -80,7 +79,7 @@ class OutgoingRequestsLog(models.Model):
verbose_name=_("Response body"),
blank=True,
null=True,
help_text=_("The response body.")
help_text=_("The response body."),
)
response_ms = models.PositiveIntegerField(
verbose_name=_("Response in ms"),
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ packages = find:
install_requires =
django >= 3.2
requests
django-solo
tests_require =
pytest
pytest-django
Expand Down

0 comments on commit 5008ad8

Please sign in to comment.