@@ -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