浏览代码

Fixed #26504 -- Avoided logging "Not Found" warnings if a middleware handles the 404.

For example, this avoids a warning in the case of a request that's
redirected to a language-prefixed URL by LocaleMiddleware.
Carl Worth 9 年之前
父节点
当前提交
40b69607c7
共有 2 个文件被更改,包括 11 次插入4 次删除
  1. 6 4
      django/core/handlers/base.py
  2. 5 0
      tests/logging_tests/tests.py

+ 6 - 4
django/core/handlers/base.py

@@ -179,10 +179,6 @@ class BaseHandler(object):
                 response_is_rendered = True
 
         except http.Http404 as exc:
-            logger.warning(
-                'Not Found: %s', request.path,
-                extra={'status_code': 404, 'request': request},
-            )
             if settings.DEBUG:
                 response = debug.technical_404_response(request, exc)
             else:
@@ -246,6 +242,12 @@ class BaseHandler(object):
         if not response_is_rendered and callable(getattr(response, 'render', None)):
             response = response.render()
 
+        if response.status_code == 404:
+            logger.warning(
+                'Not Found: %s', request.path,
+                extra={'status_code': 404, 'request': request},
+            )
+
         return response
 
     def process_exception_by_middleware(self, exception, request):

+ 5 - 0
tests/logging_tests/tests.py

@@ -133,6 +133,11 @@ class HandlerLoggingTests(SetupDefaultLoggingMixin, LoggingCaptureMixin, SimpleT
 )
 class I18nLoggingTests(SetupDefaultLoggingMixin, LoggingCaptureMixin, SimpleTestCase):
 
+    def test_i18n_page_found_no_warning(self):
+        self.client.get('/exists/')
+        self.client.get('/en/exists/')
+        self.assertEqual(self.logger_output.getvalue(), '')
+
     def test_i18n_page_not_found_warning(self):
         self.client.get('/this_does_not/')
         self.client.get('/en/nor_this/')