From 8157bba3d97b7628ca957a25ff296ac8589c1fdd Mon Sep 17 00:00:00 2001 From: Kim Gustyr Date: Thu, 7 Nov 2024 12:55:53 +0000 Subject: [PATCH] optimise log record access --- api/util/logging.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/api/util/logging.py b/api/util/logging.py index 8f2b960d3583..06361088de02 100644 --- a/api/util/logging.py +++ b/api/util/logging.py @@ -36,21 +36,22 @@ def format(self, record: logging.LogRecord) -> str: class GunicornAccessLogJsonFormatter(JsonFormatter): def get_json_record(self, record: logging.LogRecord) -> dict[str, Any]: response_time = datetime.strptime(record.args["t"], "[%d/%b/%Y:%H:%M:%S %z]") - url = record.args["U"] - if record.args["q"]: - url += f"?{record.args['q']}" + args = record.args + url = args["U"] + if q := args["q"]: + url += f"?{q}" return { **super().get_json_record(record), "time": response_time.isoformat(), "path": url, - "remote_ip": record.args["h"], - "method": record.args["m"], - "status": str(record.args["s"]), - "user_agent": record.args["a"], - "referer": record.args["f"], - "duration_in_ms": record.args["M"], - "pid": record.args["p"], + "remote_ip": args["h"], + "method": args["m"], + "status": str(args["s"]), + "user_agent": args["a"], + "referer": args["f"], + "duration_in_ms": args["M"], + "pid": args["p"], }