diff --git a/CHANGES/14390.bugfix.rst b/CHANGES/14390.bugfix.rst new file mode 100644 index 00000000000..9cd1438dc31 --- /dev/null +++ b/CHANGES/14390.bugfix.rst @@ -0,0 +1,4 @@ +Fixed the ``%%`` escape in the access log format, which made every access +log line fail with ``Error in logging``. ``%%`` followed by a format letter, +such as ``%%P``, is now logged as a literal ``%P`` instead of ``%`` and the +substituted value -- by :user:`ArnabDutta01`. diff --git a/CHANGES/14391.bugfix.rst b/CHANGES/14391.bugfix.rst new file mode 120000 index 00000000000..84b6958c4ed --- /dev/null +++ b/CHANGES/14391.bugfix.rst @@ -0,0 +1 @@ +14390.bugfix.rst \ No newline at end of file diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 753b7480ca9..d96fca40616 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -57,6 +57,7 @@ Anton Kasyanov Anton Zhdan-Pushkin Arcadiy Ivanov Arie Bovenberg +Arnab Dutta Arockia Rajamanickam Arseny Timoniq Arsh Smith diff --git a/aiohttp/web_log.py b/aiohttp/web_log.py index e75f48f31e5..96bd228f201 100644 --- a/aiohttp/web_log.py +++ b/aiohttp/web_log.py @@ -107,7 +107,9 @@ def compile_format(self, log_format: str) -> tuple[str, list[KeyMethod]]: # can repeat the same key more than once methods = list() - for atom in self.FORMAT_RE.findall(log_format): + # "%%" is a literal percent sign, so atoms are only looked up between them. + parts = log_format.split("%%") + for atom in [a for part in parts for a in self.FORMAT_RE.findall(part)]: if atom[1] == "": format_key1 = self.LOG_FORMAT_MAP[atom[0]] m = getattr(AccessLogger, "_format_%s" % atom[0]) @@ -119,9 +121,11 @@ def compile_format(self, log_format: str) -> tuple[str, list[KeyMethod]]: methods.append(key_method) - log_format = self.FORMAT_RE.sub(r"%s", log_format) - log_format = self.CLEANUP_RE.sub(r"%\1", log_format) - return log_format, methods + parts = [ + self.CLEANUP_RE.sub(r"%\1", self.FORMAT_RE.sub(r"%s", part)) + for part in parts + ] + return "%%".join(parts), methods @staticmethod def _format_i( diff --git a/tests/test_web_log.py b/tests/test_web_log.py index 0d0fac192c2..143f2d6b9ad 100644 --- a/tests/test_web_log.py +++ b/tests/test_web_log.py @@ -24,10 +24,29 @@ def test_access_logger_format() -> None: log_format = '%T "%{ETag}o" %X {X} %%P' mock_logger = mock.Mock() access_logger = AccessLogger(mock_logger, log_format) - expected = '%s "%s" %%X {X} %%%s' + expected = '%s "%s" %%X {X} %%P' assert expected == access_logger._log_format +@pytest.mark.parametrize( + ("log_format", "expected"), + [ + ("%s 100%%", "200 100%"), + ("%% %s", "% 200"), + ("%%s %s", "%s 200"), + ("%%%s", "%200"), + ("%%%% %s", "%% 200"), + ], +) +def test_access_logger_literal_percent(log_format: str, expected: str) -> None: + mock_logger = mock.Mock() + access_logger = AccessLogger(mock_logger, log_format) + response = mock.Mock(headers={}, status=200) + access_logger.log(mock.Mock(), response, 0.1) + assert not mock_logger.exception.called, mock_logger.exception.call_args + mock_logger.info.assert_called_once_with(expected, extra={"response_status": 200}) + + @pytest.mark.skipif( IS_PYPY, reason="""