Remove HomeAssistantAccessLogger (#104173)

This commit is contained in:
J. Nick Koston 2023-11-29 10:40:19 -07:00 committed by GitHub
parent 1727c19e0d
commit 1b048ff388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 47 deletions

View File

@ -6,7 +6,6 @@ import logging
from aiohttp import web from aiohttp import web
import voluptuous as vol import voluptuous as vol
from homeassistant.components.http import HomeAssistantAccessLogger
from homeassistant.components.network import async_get_source_ip from homeassistant.components.network import async_get_source_ip
from homeassistant.const import ( from homeassistant.const import (
CONF_ENTITIES, CONF_ENTITIES,
@ -101,7 +100,7 @@ async def start_emulated_hue_bridge(
config.advertise_port or config.listen_port, config.advertise_port or config.listen_port,
) )
runner = web.AppRunner(app, access_log_class=HomeAssistantAccessLogger) runner = web.AppRunner(app)
await runner.setup() await runner.setup()
site = web.TCPSite(runner, config.host_ip_addr, config.listen_port) site = web.TCPSite(runner, config.host_ip_addr, config.listen_port)

View File

@ -16,7 +16,6 @@ from aiohttp.http_parser import RawRequestMessage
from aiohttp.streams import StreamReader from aiohttp.streams import StreamReader
from aiohttp.typedefs import JSONDecoder, StrOrURL from aiohttp.typedefs import JSONDecoder, StrOrURL
from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection
from aiohttp.web_log import AccessLogger
from aiohttp.web_protocol import RequestHandler from aiohttp.web_protocol import RequestHandler
from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher
from aiohttp_zlib_ng import enable_zlib_ng from aiohttp_zlib_ng import enable_zlib_ng
@ -238,25 +237,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
class HomeAssistantAccessLogger(AccessLogger):
"""Access logger for Home Assistant that does not log when disabled."""
def log(
self, request: web.BaseRequest, response: web.StreamResponse, time: float
) -> None:
"""Log the request.
The default implementation logs the request to the logger
with the INFO level and than throws it away if the logger
is not enabled for the INFO level. This implementation
does not log the request if the logger is not enabled for
the INFO level.
"""
if not self.logger.isEnabledFor(logging.INFO):
return
super().log(request, response, time)
class HomeAssistantRequest(web.Request): class HomeAssistantRequest(web.Request):
"""Home Assistant request object.""" """Home Assistant request object."""
@ -540,9 +520,7 @@ class HomeAssistantHTTP:
# pylint: disable-next=protected-access # pylint: disable-next=protected-access
self.app._router.freeze = lambda: None # type: ignore[method-assign] self.app._router.freeze = lambda: None # type: ignore[method-assign]
self.runner = web.AppRunner( self.runner = web.AppRunner(self.app)
self.app, access_log_class=HomeAssistantAccessLogger
)
await self.runner.setup() await self.runner.setup()
self.site = HomeAssistantTCPSite( self.site = HomeAssistantTCPSite(

View File

@ -5,8 +5,7 @@ from http import HTTPStatus
from ipaddress import ip_network from ipaddress import ip_network
import logging import logging
from pathlib import Path from pathlib import Path
import time from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch
import pytest import pytest
@ -21,7 +20,6 @@ from homeassistant.util import dt as dt_util
from homeassistant.util.ssl import server_context_intermediate, server_context_modern from homeassistant.util.ssl import server_context_intermediate, server_context_modern
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
from tests.test_util.aiohttp import AiohttpClientMockResponse
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
@ -501,22 +499,3 @@ async def test_logging(
response = await client.get("/api/states/logging.entity") response = await client.get("/api/states/logging.entity")
assert response.status == HTTPStatus.OK assert response.status == HTTPStatus.OK
assert "GET /api/states/logging.entity" not in caplog.text assert "GET /api/states/logging.entity" not in caplog.text
async def test_hass_access_logger_at_info_level(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that logging happens at info level."""
test_logger = logging.getLogger("test.aiohttp.logger")
logger = http.HomeAssistantAccessLogger(test_logger)
mock_request = MagicMock()
response = AiohttpClientMockResponse(
"POST", "http://127.0.0.1", status=HTTPStatus.OK
)
setattr(response, "body_length", 42)
logger.log(mock_request, response, time.time())
assert "42" in caplog.text
caplog.clear()
test_logger.setLevel(logging.WARNING)
logger.log(mock_request, response, time.time())
assert "42" not in caplog.text