Skip to content

Commit

Permalink
Do not log querystrings in request.summary logs (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem authored May 16, 2024
1 parent 8d788e4 commit 231a705
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions telescope/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion telescope/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_basic_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 231a705

Please sign in to comment.