Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/104 #105

Merged
merged 5 commits into from
Aug 28, 2020
Merged

Issues/104 #105

merged 5 commits into from
Aug 28, 2020

Conversation

KSD-France
Copy link
Contributor

@KSD-France KSD-France commented Aug 21, 2020

Fixes #104.


if i != last:
part = part + self.boundary

self.logger.log(self.log_level, part, logging_context)

def _log_resp(self, level, response, logging_context):
if re.match('^application/json', response.get('Content-Type', ''), re.I):
if re.match("^application/json", response.get("Content-Type", ""), re.I):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformatting everything really requires calling out the changes so I don't wind up questioning all our previous decisions :)

Is there any reason to use re.match here instead of a string find on the lower-cased content-type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformatting everything really requires calling out the changes so I don't wind up questioning all our previous decisions :)

Same problem here :D

Is there any reason to use re.match here instead of a string find on the lower-cased content-type?

I don't see any. IMHO, it would even deserve a more stringent test (if response.get("Content-Type", "").lower() == "application/json"), unless you want to support all JSON variants out there (boy there are quite a few!).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that application/json; charset=UTF-8 is a valid content type (albeit obsolete), the test should probably be: if response.get("Content-Type", "").lower().startswith("application/json").

from django.conf import settings
from django.test import RequestFactory, override_settings
from django.http import HttpResponse, StreamingHttpResponse

import request_logging
from request_logging.middleware import LoggingMiddleware, DEFAULT_LOG_LEVEL, DEFAULT_COLORIZE, DEFAULT_MAX_BODY_LENGTH,\
NO_LOGGING_MSG, DEFAULT_HTTP_4XX_LOG_LEVEL
from request_logging.middleware import (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any additional tests here to prove the new functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add some and commit.

@KSD-France
Copy link
Contributor Author

Just added tests. Not sure I followed the right pattern though, let me know!

@KSD-France KSD-France marked this pull request as draft August 21, 2020 14:53
@KSD-France
Copy link
Contributor Author

Manual tests reveal that proposed changes break: submitting a form triggers the following error:

Traceback (most recent call last):
  File "*****/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 52, in inner
    response = get_response(request)
  File "*****/venv/lib/python3.8/site-packages/request_logging/middleware.py", line 112, in __call__
    self.process_request(request, response)
  File "*****/venv/lib/python3.8/site-packages/request_logging/middleware.py", line 122, in process_request
    return self._log_request(request, response)
  File "*****/venv/lib/python3.8/site-packages/request_logging/middleware.py", line 177, in _log_request
    self._log_request_body(request, logging_context, log_level)
  File "*****/venv/lib/python3.8/site-packages/request_logging/middleware.py", line 190, in _log_request_body
    if request.body:
  File "*****/venv/lib/python3.8/site-packages/django/http/request.py", line 314, in body
    raise RawPostDataException("You cannot access body after reading from request's data stream")

Will investigate more next week.

@KSD-France
Copy link
Contributor Author

@tclancy

I see 2 possible refactorings of LoggingMiddleware._log_request_body:

  1. Simplest workaround would be to cache request.body before the request is processed and accessing this cached version in _log_request_body, yielding same log results as before.
  2. More involved refactoring would be to test request's method (GET/POST/...) and log the cognate request's attribute, ie: if processing a POST request, log request.POST (instead of raw request.body).

Your choice!

@tclancy
Copy link
Collaborator

tclancy commented Aug 25, 2020

I think the first is simplest, let's try that.

@KSD-France KSD-France marked this pull request as ready for review August 25, 2020 13:47
@KSD-France
Copy link
Contributor Author

OK, done. I think this might be ready for review/merge. Testing on our current project right now.

@tclancy
Copy link
Collaborator

tclancy commented Aug 25, 2020

Let me know when you're done with your testing; things are pretty crazy here this week so I won't have much of a chance to look at it.

@KSD-France
Copy link
Contributor Author

OK, things look pretty smooth so far, let's just give it some time and use and I'll let you know by the end of the week :-)

@KSD-France
Copy link
Contributor Author

Manual testing revealed no issue during the week. I didn't have the opportunity to test with DRF though.

@KSD-France
Copy link
Contributor Author

Nevermind. Just tested on another project that uses DRF and it's all good (both 200 and >= 400 requests).

@tclancy tclancy merged commit 9bfe9c9 into Rhumbix:master Aug 28, 2020
@KSD-France KSD-France deleted the issues/104 branch August 31, 2020 06:36
@KSD-France
Copy link
Contributor Author

Thanks for merging 👍

@KSD-France
Copy link
Contributor Author

Sorry to revive this PR, just want to know if you have plans to package and deploy to Pypi an updated version including these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Log request headers and body at 4xx level when response status is >= 400
2 participants