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
import voluptuous as vol
from homeassistant.components.http import HomeAssistantAccessLogger
from homeassistant.components.network import async_get_source_ip
from homeassistant.const import (
CONF_ENTITIES,
@ -101,7 +100,7 @@ async def start_emulated_hue_bridge(
config.advertise_port or config.listen_port,
)
runner = web.AppRunner(app, access_log_class=HomeAssistantAccessLogger)
runner = web.AppRunner(app)
await runner.setup()
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.typedefs import JSONDecoder, StrOrURL
from aiohttp.web_exceptions import HTTPMovedPermanently, HTTPRedirection
from aiohttp.web_log import AccessLogger
from aiohttp.web_protocol import RequestHandler
from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher
from aiohttp_zlib_ng import enable_zlib_ng
@ -238,25 +237,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
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):
"""Home Assistant request object."""
@ -540,9 +520,7 @@ class HomeAssistantHTTP:
# pylint: disable-next=protected-access
self.app._router.freeze = lambda: None # type: ignore[method-assign]
self.runner = web.AppRunner(
self.app, access_log_class=HomeAssistantAccessLogger
)
self.runner = web.AppRunner(self.app)
await self.runner.setup()
self.site = HomeAssistantTCPSite(

View file

@ -5,8 +5,7 @@ from http import HTTPStatus
from ipaddress import ip_network
import logging
from pathlib import Path
import time
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import Mock, patch
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 tests.common import async_fire_time_changed
from tests.test_util.aiohttp import AiohttpClientMockResponse
from tests.typing import ClientSessionGenerator
@ -501,22 +499,3 @@ async def test_logging(
response = await client.get("/api/states/logging.entity")
assert response.status == HTTPStatus.OK
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