Log messages related to the handling of requests. 5XX responses are
raised as ERROR messages; 4XX responses are raised as WARNING messages.
The actual behaviour is not quite consistent with this:
- Only `404` and `500` responses are logged to `django.request` - not any
other responses in the `4xx` and `5xx` range.
- `500` responses are only logged if they are the result of an uncaught
exception. The logging happens in
`django.core.handlers.base.handle_uncaught_exception`. If a view manually
sets a `500` response somewhere, this isn't logged.
- The same was true of `404` responses (they were only logged if an
uncaught `Http404` was raised), until the change made in ticket:26504
inadvertently altered this behaviour. After that change, all `404`s are
logged regardless of how they are generated.
I would be happy to submit a patch that addresses this but would like some
guidance on what the best solution is. My initial thoughts are:
- I think Django should log all `5xx` responses, regardless of how they
were generated. This would mean refactoring the logic that does this
logging.
- Either log all `4xx` responses or change the documentation to say that
only `404`s are logged.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> The documentation for `django.request` says:
>
> Log messages related to the handling of requests. 5XX responses are
> raised as ERROR messages; 4XX responses are raised as WARNING messages.
>
> The actual behaviour is not quite consistent with this:
>
> - Only `404` and `500` responses are logged to `django.request` - not any
> other responses in the `4xx` and `5xx` range.
>
> - `500` responses are only logged if they are the result of an uncaught
> exception. The logging happens in
> `django.core.handlers.base.handle_uncaught_exception`. If a view manually
> sets a `500` response somewhere, this isn't logged.
>
> - The same was true of `404` responses (they were only logged if an
> uncaught `Http404` was raised), until the change made in ticket:26504
> inadvertently altered this behaviour. After that change, all `404`s are
> logged regardless of how they are generated.
>
> I would be happy to submit a patch that addresses this but would like
> some guidance on what the best solution is. My initial thoughts are:
>
> - I think Django should log all `5xx` responses, regardless of how they
> were generated. This would mean refactoring the logic that does this
> logging.
>
> - Either log all `4xx` responses or change the documentation to say that
> only `404`s are logged.
New description:
The documentation for `django.request` says:
Log messages related to the handling of requests. 5XX responses are
raised as ERROR messages; 4XX responses are raised as WARNING messages.
The actual behaviour is not quite consistent with this:
- Only `4xx` and `500` responses are logged to `django.request` - not any
other responses in the `5xx` range.
- `500` responses are only logged if they are the result of an uncaught
exception. The logging happens in
`django.core.handlers.base.handle_uncaught_exception`. If a view manually
sets a `500` response somewhere, this isn't logged.
- The same was true of `404` responses (they were only logged if an
uncaught `Http404` was raised), until the change made in ticket:26504
inadvertently altered this behaviour. After that change, all `404`s are
logged regardless of how they are generated.
- Other `4xx` responses meanwhile are only logged as the result of an
exception (`PermissionDenied` etc.), not if the status code is set
manually.
- `400` responses are logged to `django.security` but not to
`django.request`.
I would be happy to submit a patch that addresses this but would like some
guidance on what the best solution is. My initial thoughts are:
- I think Django should log all `5xx` responses, regardless of how they
were generated. This would mean refactoring the logic that does this
logging.
- Either log all `4xx` responses to `django.request` or change the
documentation to list what specific responses are logged.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:1>
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
Comment:
The logging behavior is clearly inconsistent with the docs. The documented
behavior is preferable over the current one so that's actually a bug.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:2>
* owner: nobody => seocam
* cc: seocam@… (added)
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:3>
* has_patch: 0 => 1
Comment:
https://github.com/django/django/pull/6716
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:5>
* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted
Comment:
Patch needs a rebase and I left some additional comments for improvement.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:6>
Comment (by Samir Shah):
I've taken the liberty of updating the original PR to work with changes in
the code base since it was created -
https://github.com/django/django/pull/8752. Most of the original work way
done by Sergio.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:7>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:8>
* needs_better_patch: 0 => 1
Comment:
This looks good to me. I had a minor documentation comment on the patch
review.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:9>
* needs_better_patch: 1 => 0
Comment:
Tim - thanks for the review. I've updated the
[https://github.com/django/django/pull/8752 PR] to address your comments.
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:10>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"10b44e45256ddda4258ae032b8d4725a3e3284e6" 10b44e45]:
{{{
#!CommitTicketReference repository=""
revision="10b44e45256ddda4258ae032b8d4725a3e3284e6"
Fixed #26688 -- Fixed HTTP request logging inconsistencies.
* Added logging of 500 responses for instantiated responses.
* Added logging of all 4xx and 5xx responses.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26688#comment:12>