From 231a705783a3c2d3d48443336435dd23c07e74ea Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Thu, 16 May 2024 15:44:43 +0200 Subject: [PATCH] Do not log querystrings in request.summary logs (#1433) --- telescope/config.py | 1 + telescope/middleware.py | 6 +++++- tests/test_basic_endpoints.py | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/telescope/config.py b/telescope/config.py index 3b25fc34..fc652132 100644 --- a/telescope/config.py +++ b/telescope/config.py @@ -44,6 +44,7 @@ VERSION_FILE = config("VERSION_FILE", default="version.json") LOG_LEVEL = config("LOG_LEVEL", default="INFO").upper() LOG_FORMAT = config("LOG_FORMAT", default="json") +LOG_SUMMARY_QUERYSTRING = config("LOG_SUMMARY_QUERYSTRING", default=False) LOGGING = { "version": 1, "formatters": { diff --git a/telescope/middleware.py b/telescope/middleware.py index b02a969b..ea7190bd 100644 --- a/telescope/middleware.py +++ b/telescope/middleware.py @@ -7,6 +7,8 @@ from aiohttp import web from aiohttp.web import middleware +from . import config + logger = logging.getLogger(__name__) summary_logger = logging.getLogger("request.summary") @@ -21,11 +23,13 @@ async def request_summary(request, handler): "path": str(request.rel_url), "method": request.method, "lang": request.headers.get("Accept-Language"), - "querystring": dict(request.query), "errno": 0, "rid": request.headers.get("X-Request-Id", token_hex(16)), } + if config.LOG_SUMMARY_QUERYSTRING: + infos["querystring"] = dict(request.query) + response = await handler(request) current = time.time() diff --git a/tests/test_basic_endpoints.py b/tests/test_basic_endpoints.py index 1ab490da..3fde3f0c 100644 --- a/tests/test_basic_endpoints.py +++ b/tests/test_basic_endpoints.py @@ -356,6 +356,25 @@ def callback(event_type, payload): ] +async def test_logging_summary_no_querystring_by_default(caplog, cli): + caplog.set_level(logging.INFO, logger="request.summary") + + await cli.get("/?foo=bar") + + [summary_log] = [log for log in caplog.records if log.name == "request.summary"] + assert not hasattr(summary_log, "querystring") + + +async def test_logging_summary_with_querystring_if_enabled(caplog, config, cli): + caplog.set_level(logging.INFO, logger="request.summary") + config.LOG_SUMMARY_QUERYSTRING = True + + await cli.get("/?foo=bar") + + [summary_log] = [log for log in caplog.records if log.name == "request.summary"] + assert summary_log.querystring == {"foo": "bar"} + + async def test_logging_result(caplog, cli, mock_aioresponses): cli.app["telescope.cache"] = None caplog.set_level(logging.INFO, logger="check.result")