mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 11:16:47 +00:00
fix(logs): a 404 is not written to the app log
Django logs every 4xx as a django.request WARNING; each 404 of a scanner or an old link was a line in the app log, burying the ones that matter. 404s are left out; 403s and errors stay (a refused page also has a Logs row).
This commit is contained in:
parent
45828928ab
commit
a0d8bcca89
3 changed files with 21 additions and 0 deletions
|
|
@ -22,3 +22,11 @@ class RequestFilter(logging.Filter):
|
|||
except Exception: # the user may be read from the database here; a failure must not lose the line
|
||||
pass
|
||||
return True
|
||||
|
||||
|
||||
class NotFoundFilter(logging.Filter):
|
||||
"""Leaves out django.request's line for a 404: scanners and old links
|
||||
would bury the lines that matter."""
|
||||
|
||||
def filter(self, record):
|
||||
return getattr(record, "status_code", None) != 404
|
||||
|
|
|
|||
|
|
@ -256,6 +256,7 @@ LOGGING = {
|
|||
},
|
||||
'filters': {
|
||||
'request': {'()': 'webvirtcloud.log_context.RequestFilter'},
|
||||
'not_found': {'()': 'webvirtcloud.log_context.NotFoundFilter'},
|
||||
},
|
||||
'handlers': {
|
||||
'mail_admins': {
|
||||
|
|
@ -282,6 +283,7 @@ LOGGING = {
|
|||
"handlers": ["mail_admins"],
|
||||
"level": "ERROR",
|
||||
"propagate": True,
|
||||
"filters": ["not_found"],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,3 +72,14 @@ class RequestIdTestCase(TestCase):
|
|||
def test_an_id_sent_by_a_client_cannot_write_into_the_log(self):
|
||||
response = self.client.get("/accounts/login/", HTTP_X_REQUEST_ID="x\n[forged] line")
|
||||
self.assertRegex(response["X-Request-ID"], r"^[0-9a-f]{32}$")
|
||||
|
||||
|
||||
class NotFoundTestCase(TestCase):
|
||||
def test_a_404_is_not_logged_and_a_403_is(self):
|
||||
self.client.force_login(User.objects.create_user("plain"))
|
||||
with self.assertLogs("django.request", "WARNING") as logs:
|
||||
self.client.get("/no-such-page/")
|
||||
self.client.get("/admin/users/")
|
||||
logging.getLogger("django.request").warning("end") # assertLogs needs one line
|
||||
self.assertEqual([line.split(":")[0] for line in logs.output], ["WARNING", "WARNING"])
|
||||
self.assertIn("Forbidden", logs.output[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue