From 89bb696782f51b2a936b1ea49a4c7b40c2372c34 Mon Sep 17 00:00:00 2001 From: ArnabDutta01 Date: Mon, 16 Nov 2026 11:20:01 +0530 Subject: [PATCH 1/3] Fix the %% escape in the access log format A literal "%%" in the access log format was not treated as an escape: the cleanup step turned it into "%%%", so every log line failed with "Error in logging". The "%" that followed it was also taken as the start of an atom, so "%%P" logged the process id instead of "%P". Split the format on "%%" before looking up atoms and join the compiled parts back with "%%". Signed-off-by: ArnabDutta01 --- CHANGES/14390.bugfix.rst | 2 ++ CONTRIBUTORS.txt | 1 + aiohttp/web_log.py | 12 ++++++++---- tests/test_web_log.py | 21 ++++++++++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 CHANGES/14390.bugfix.rst diff --git a/CHANGES/14390.bugfix.rst b/CHANGES/14390.bugfix.rst new file mode 100644 index 00000000000..96bcca60211 --- /dev/null +++ b/CHANGES/14390.bugfix.rst @@ -0,0 +1,2 @@ +Fixed the ``%%`` escape in the access log format, which made every access +log line fail with ``Error in logging`` -- by :user:`ArnabDutta01`. 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=""" From d3e6278e0ff117d8c206c90632179bd3f9e902fc Mon Sep 17 00:00:00 2001 From: ArnabDutta01 Date: Tue, 17 Nov 2026 01:02:07 +0530 Subject: [PATCH 2/3] Add PR-number release note symlink Signed-off-by: ArnabDutta01 --- CHANGES/14391.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 120000 CHANGES/14391.bugfix.rst 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 From 7c06dba2e144f4c77fd1f691ed166537fbe1abab Mon Sep 17 00:00:00 2001 From: ArnabDutta01 Date: Thu, 3 Dec 2026 19:55:39 +0530 Subject: [PATCH 3/3] Mention the %%P change in the release note Signed-off-by: ArnabDutta01 --- CHANGES/14390.bugfix.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES/14390.bugfix.rst b/CHANGES/14390.bugfix.rst index 96bcca60211..9cd1438dc31 100644 --- a/CHANGES/14390.bugfix.rst +++ b/CHANGES/14390.bugfix.rst @@ -1,2 +1,4 @@ Fixed the ``%%`` escape in the access log format, which made every access -log line fail with ``Error in logging`` -- by :user:`ArnabDutta01`. +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`.