Skip to content

Commit 5a62ec9

Browse files
[PR #10332/a0e64bb4 backport][3.12] Log the remote that generates request errors (#10338)
1 parent 016cbae commit 5a62ec9

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

CHANGES/10332.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved logging of HTTP protocol errors to include the remote address -- by :user:`bdraco`.

aiohttp/web_protocol.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,13 @@ def handle_error(
694694
# or encrypted traffic to an HTTP port. This is expected
695695
# to happen when connected to the public internet so we log
696696
# it at the debug level as to not fill logs with noise.
697-
self.logger.debug("Error handling request", exc_info=exc)
697+
self.logger.debug(
698+
"Error handling request from %s", request.remote, exc_info=exc
699+
)
698700
else:
699-
self.log_exception("Error handling request", exc_info=exc)
701+
self.log_exception(
702+
"Error handling request from %s", request.remote, exc_info=exc
703+
)
700704

701705
# some data already got sent, connection is broken
702706
if request.writer.output_size > 0:

tests/test_web_server.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ async def handler(request):
5656
assert txt.startswith("500 Internal Server Error")
5757
assert "Traceback" not in txt
5858

59-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
59+
logger.exception.assert_called_with(
60+
"Error handling request from %s", cli.host, exc_info=exc
61+
)
6062

6163

6264
async def test_raw_server_logs_invalid_method_with_loop_debug(
@@ -85,7 +87,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
8587
# on the first request since the client may
8688
# be probing for TLS/SSL support which is
8789
# expected to fail
88-
logger.debug.assert_called_with("Error handling request", exc_info=exc)
90+
logger.debug.assert_called_with(
91+
"Error handling request from %s", cli.host, exc_info=exc
92+
)
8993
logger.debug.reset_mock()
9094

9195
# Now make another connection to the server
@@ -99,7 +103,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
99103
# on the first request since the client may
100104
# be probing for TLS/SSL support which is
101105
# expected to fail
102-
logger.debug.assert_called_with("Error handling request", exc_info=exc)
106+
logger.debug.assert_called_with(
107+
"Error handling request from %s", cli.host, exc_info=exc
108+
)
103109

104110

105111
async def test_raw_server_logs_invalid_method_without_loop_debug(
@@ -128,7 +134,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
128134
# on the first request since the client may
129135
# be probing for TLS/SSL support which is
130136
# expected to fail
131-
logger.debug.assert_called_with("Error handling request", exc_info=exc)
137+
logger.debug.assert_called_with(
138+
"Error handling request from %s", cli.host, exc_info=exc
139+
)
132140

133141

134142
async def test_raw_server_logs_invalid_method_second_request(
@@ -159,7 +167,9 @@ async def handler(request: web.BaseRequest) -> web.Response:
159167
# BadHttpMethod should be logged as an exception
160168
# if its not the first request since we know
161169
# that the client already was speaking HTTP
162-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
170+
logger.exception.assert_called_with(
171+
"Error handling request from %s", cli.host, exc_info=exc
172+
)
163173

164174

165175
async def test_raw_server_logs_bad_status_line_as_exception(
@@ -184,7 +194,9 @@ async def handler(request: web.BaseRequest) -> NoReturn:
184194
txt = await resp.text()
185195
assert "Traceback (most recent call last):\n" not in txt
186196

187-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
197+
logger.exception.assert_called_with(
198+
"Error handling request from %s", cli.host, exc_info=exc
199+
)
188200

189201

190202
async def test_raw_server_handler_timeout(
@@ -254,7 +266,9 @@ async def handler(request):
254266
txt = await resp.text()
255267
assert "Traceback (most recent call last):\n" in txt
256268

257-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
269+
logger.exception.assert_called_with(
270+
"Error handling request from %s", cli.host, exc_info=exc
271+
)
258272

259273

260274
async def test_raw_server_html_exception(aiohttp_raw_server, aiohttp_client):
@@ -278,7 +292,9 @@ async def handler(request):
278292
"</body></html>\n"
279293
)
280294

281-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
295+
logger.exception.assert_called_with(
296+
"Error handling request from %s", cli.host, exc_info=exc
297+
)
282298

283299

284300
async def test_raw_server_html_exception_debug(aiohttp_raw_server, aiohttp_client):
@@ -302,7 +318,9 @@ async def handler(request):
302318
"<pre>Traceback (most recent call last):\n"
303319
)
304320

305-
logger.exception.assert_called_with("Error handling request", exc_info=exc)
321+
logger.exception.assert_called_with(
322+
"Error handling request from %s", cli.host, exc_info=exc
323+
)
306324

307325

308326
async def test_handler_cancellation(unused_port_socket: socket.socket) -> None:

0 commit comments

Comments
 (0)