diff --git a/src/__pycache__/logging.cpython-311.pyc b/src/__pycache__/logging.cpython-311.pyc index c8d8b12..20fbd26 100644 Binary files a/src/__pycache__/logging.cpython-311.pyc and b/src/__pycache__/logging.cpython-311.pyc differ diff --git a/src/__pycache__/main.cpython-311.pyc b/src/__pycache__/main.cpython-311.pyc index 33c1c5f..e7365ad 100644 Binary files a/src/__pycache__/main.cpython-311.pyc and b/src/__pycache__/main.cpython-311.pyc differ diff --git a/src/logging.py b/src/logging.py index a268c3e..a7f31af 100644 --- a/src/logging.py +++ b/src/logging.py @@ -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: diff --git a/src/main.py b/src/main.py index fd54353..b5f803b 100644 --- a/src/main.py +++ b/src/main.py @@ -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()