This commit is contained in:
2026-04-25 16:31:07 +09:00
parent 41b0cc6dd6
commit 9d61a19a6e
3 changed files with 34 additions and 25 deletions
+8 -2
View File
@@ -7,8 +7,7 @@ from datetime import datetime, timezone
access_log_path = Path.cwd().joinpath("logs", "access.log")
access_log_path.parent.mkdir(parents=True, exist_ok=True)
# TODO: ステータスコードとかも含めたい でもそれにはレスポンスが出来上がってからログを書く必要があるので/echoとかを残すには工夫がいりそう
def log_access(scope: Scope, write: bool = True):
def log_access(scope: Scope, write: bool = False):
client = scope.get("client") or ("", 0)
server = scope.get("server") or ("", 0)
headers = dict(scope.get("headers", []))
@@ -33,3 +32,10 @@ def log_access(scope: Scope, write: bool = True):
with access_log_path.open("a", encoding="utf-8") as f:
f.write(json.dumps(log, ensure_ascii=False) + "\n")
return log
def finalize_log(log: dict, status_code: int, write: bool = True) -> None:
log["status_code"] = status_code
if write:
with access_log_path.open("a", encoding="utf-8") as f:
f.write(json.dumps(log, ensure_ascii=False) + "\n")
return log