feat: Enhance logging by defaulting request ID to SYSTEM, filtering internal attributes, and adding request lifecycle logs in the middleware.

main
MrWaradana 4 weeks ago
parent c81bb21ef5
commit 8797b8cd96

@ -54,7 +54,7 @@ class JSONFormatter(logging.Formatter):
"funcName": record.funcName,
"lineno": record.lineno,
"pid": os.getpid(),
"request_id": request_id,
"request_id": request_id or "SYSTEM", # request id assigned per request or SYSTEM for system logs
}
@ -67,12 +67,13 @@ class JSONFormatter(logging.Formatter):
log_record["stack_trace"] = self.formatStack(record.stack_info)
# Add any extra attributes passed to the log call
# We skip standard attributes to avoid duplication
# We skip standard and internal uvicorn/fastapi attributes to avoid duplication or mess
standard_attrs = {
"args", "asctime", "created", "exc_info", "exc_text", "filename",
"funcName", "levelname", "levelno", "lineno", "module", "msecs",
"message", "msg", "name", "pathname", "process", "processName",
"relativeCreated", "stack_info", "thread", "threadName"
"relativeCreated", "stack_info", "thread", "threadName",
"color_message", "request", "scope"
}
for key, value in record.__dict__.items():
if key not in standard_attrs:

@ -65,12 +65,15 @@ async def db_session_middleware(request: Request, call_next):
try:
log.info(f"Incoming request: {request.method} {request.url.path}")
session = async_scoped_session(async_session, scopefunc=get_request_id)
request.state.db = session()
collector_session = async_scoped_session(collector_async_session, scopefunc=get_request_id)
request.state.collector_db = collector_session()
response = await call_next(request)
log.info(f"Request completed: {response.status_code}")
except Exception as e:
log.error(f"Request failed: {type(e).__name__} - {str(e)}")
raise e from None
finally:
await request.state.db.close()

Loading…
Cancel
Save