Skip to content

Commit b796fce

Browse files
[PR #11795/d0970585 backport][3.14] Added regression test for cached logging status (#11803)
**This is a backport of PR #11795 as merged into master (d097058).** --------- Co-authored-by: meehand <[email protected]>
1 parent 149dfa7 commit b796fce

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CHANGES/11778.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added regression test for cached logging status -- by :user:`meehand`.

tests/test_web_log.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,46 @@ def log(
281281
resp = await client.get("/")
282282
assert 200 == resp.status
283283
assert "This should not be logged" not in caplog.text
284+
285+
286+
@pytest.mark.xfail(reason="#11778")
287+
async def test_logger_does_not_log_when_enabled_post_init(
288+
aiohttp_server: AiohttpServer,
289+
aiohttp_client: AiohttpClient,
290+
caplog: pytest.LogCaptureFixture,
291+
) -> None:
292+
"""Test logger does nothing when not enabled even if enabled post init."""
293+
294+
async def handler(request: web.Request) -> web.Response:
295+
return web.Response()
296+
297+
enabled = False
298+
299+
class Logger(AbstractAccessLogger):
300+
301+
def log(
302+
self, request: web.BaseRequest, response: web.StreamResponse, time: float
303+
) -> None:
304+
self.logger.critical("This should not be logged") # pragma: no cover
305+
306+
@property
307+
def enabled(self) -> bool:
308+
"""Check if logger is enabled."""
309+
# Avoid formatting the log line if it will not be emitted.
310+
return enabled
311+
312+
app = web.Application()
313+
app.router.add_get("/", handler)
314+
server = await aiohttp_server(app, access_log_class=Logger)
315+
client = await aiohttp_client(server)
316+
resp = await client.get("/")
317+
assert 200 == resp.status
318+
assert "This should not be logged" not in caplog.text
319+
assert not server.handler.connections[0]._force_close
320+
321+
# mock enabling logging post-init
322+
enabled = True
323+
resp = await client.get("/")
324+
assert 200 == resp.status
325+
assert "This should not be logged" not in caplog.text
326+
assert not server.handler.connections[0]._force_close

0 commit comments

Comments
 (0)