Spaces:
Runtime error
Runtime error
Download app/core/error_reporting.py from junaidte14/aiverse: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/spaces/junaidte14/aiverse/resolve/main/app/core/error_reporting.py
- Command line
-
hf download hf://spaces/junaidte14/aiverse/app/core/error_reporting.py
-
curl -L -o error_reporting.py https://huggingface.co/spaces/junaidte14/aiverse/resolve/main/app/core/error_reporting.py
10 kB
| # app/core/error_reporting.py | |
| """ | |
| Centralized logging + error reporting + exception handlers | |
| Production-ready logging architecture: | |
| - Clean console logs | |
| - Detailed rotating file logs | |
| - Centralized exception handling | |
| - Reduced third-party noise | |
| - Request tracing support | |
| """ | |
| import logging | |
| import time | |
| import uuid | |
| from datetime import datetime | |
| from logging.handlers import RotatingFileHandler | |
| from pathlib import Path | |
| from typing import Union | |
| from fastapi import Request, status | |
| from fastapi.encoders import jsonable_encoder | |
| from fastapi.exceptions import RequestValidationError | |
| from fastapi.responses import JSONResponse | |
| from pydantic import ValidationError | |
| from app.core.config import settings | |
| from app.core.exceptions import AppException | |
| from app.models.error import ( | |
| ErrorResponse, | |
| ValidationErrorResponse, | |
| ValidationError as ValidationErrorModel, | |
| ) | |
| # ========================================================= | |
| # LOG DIRECTORY | |
| # ========================================================= | |
| LOG_DIR = Path("logs") | |
| LOG_DIR.mkdir(exist_ok=True) | |
| # ========================================================= | |
| # LOGGER CONFIGURATION | |
| # ========================================================= | |
| LOG_FORMAT = ( | |
| "%(asctime)s | " | |
| "%(levelname)s | " | |
| "%(name)s | " | |
| "%(message)s" | |
| ) | |
| logger = logging.getLogger("app") | |
| # Prevent duplicate handlers during reloads | |
| if not logger.handlers: | |
| #logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO) - if you dont need in production | |
| logger.setLevel(logging.DEBUG) | |
| # ===================================================== | |
| # CONSOLE HANDLER (CLEAN OUTPUT) | |
| # ===================================================== | |
| console_handler = logging.StreamHandler() | |
| console_handler.setLevel(logging.INFO) | |
| console_formatter = logging.Formatter( | |
| "%(levelname)s | %(message)s" | |
| ) | |
| console_handler.setFormatter(console_formatter) | |
| # ===================================================== | |
| # MAIN FILE HANDLER (DETAILED LOGS) | |
| # ===================================================== | |
| file_handler = RotatingFileHandler( | |
| filename=LOG_DIR / "app.log", | |
| maxBytes=10 * 1024 * 1024, # 10MB | |
| backupCount=5, | |
| encoding="utf-8", | |
| ) | |
| file_handler.setLevel(logging.DEBUG) | |
| file_formatter = logging.Formatter(LOG_FORMAT) | |
| file_handler.setFormatter(file_formatter) | |
| # ===================================================== | |
| # ERROR FILE HANDLER (ERRORS ONLY) | |
| # ===================================================== | |
| error_handler = RotatingFileHandler( | |
| filename=LOG_DIR / "errors.log", | |
| maxBytes=5 * 1024 * 1024, # 5MB | |
| backupCount=3, | |
| encoding="utf-8", | |
| ) | |
| error_handler.setLevel(logging.ERROR) | |
| error_handler.setFormatter(file_formatter) | |
| # ===================================================== | |
| # ATTACH HANDLERS | |
| # ===================================================== | |
| logger.addHandler(console_handler) | |
| logger.addHandler(file_handler) | |
| logger.addHandler(error_handler) | |
| # Prevent duplicate propagation | |
| logger.propagate = False | |
| # ========================================================= | |
| # REDUCE NOISY THIRD-PARTY LOGS | |
| # ========================================================= | |
| logging.getLogger("uvicorn.access").setLevel(logging.WARNING) | |
| logging.getLogger("uvicorn.error").setLevel(logging.INFO) | |
| logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING) | |
| logging.getLogger("sqlalchemy.pool").setLevel(logging.WARNING) | |
| logging.getLogger("httpx").setLevel(logging.WARNING) | |
| logging.getLogger("httpcore").setLevel(logging.WARNING) | |
| logging.getLogger("asyncio").setLevel(logging.WARNING) | |
| # ========================================================= | |
| # REQUEST LOGGING MIDDLEWARE | |
| # ========================================================= | |
| async def request_logging_middleware( | |
| request: Request, | |
| call_next, | |
| ): | |
| """ | |
| Clean request logging middleware | |
| Console: | |
| INFO | POST /api/v1/auth/login | 200 | 145ms | |
| File logs contain additional metadata | |
| """ | |
| request_id = str(uuid.uuid4())[:8] | |
| start_time = time.perf_counter() | |
| request.state.request_id = request_id | |
| try: | |
| response = await call_next(request) | |
| duration_ms = round( | |
| (time.perf_counter() - start_time) * 1000 | |
| ) | |
| log_message = ( | |
| f"{request.method} " | |
| f"{request.url.path} | " | |
| f"{response.status_code} | " | |
| f"{duration_ms}ms" | |
| ) | |
| logger.info(log_message) | |
| # Detailed file log | |
| logger.debug( | |
| f"REQUEST_ID={request_id} | " | |
| f"METHOD={request.method} | " | |
| f"PATH={request.url.path} | " | |
| f"STATUS={response.status_code} | " | |
| f"DURATION_MS={duration_ms} | " | |
| f"CLIENT={request.client.host if request.client else 'unknown'}" | |
| ) | |
| response.headers["X-Request-ID"] = request_id | |
| return response | |
| except Exception: | |
| duration_ms = round( | |
| (time.perf_counter() - start_time) * 1000 | |
| ) | |
| logger.error( | |
| f"{request.method} " | |
| f"{request.url.path} | " | |
| f"500 | " | |
| f"{duration_ms}ms" | |
| ) | |
| raise | |
| # ========================================================= | |
| # APP EXCEPTION HANDLER | |
| # ========================================================= | |
| async def app_exception_handler( | |
| request: Request, | |
| exc: AppException, | |
| ) -> JSONResponse: | |
| """ | |
| Handles known application exceptions | |
| """ | |
| request_id = getattr( | |
| request.state, | |
| "request_id", | |
| "unknown" | |
| ) | |
| logger.warning( | |
| f"{exc.error_code} | " | |
| f"{request.method} " | |
| f"{request.url.path}" | |
| ) | |
| logger.debug( | |
| f"APP_EXCEPTION | " | |
| f"REQUEST_ID={request_id} | " | |
| f"TYPE={type(exc).__name__} | " | |
| f"DETAIL={exc.detail}" | |
| ) | |
| error_response = ErrorResponse( | |
| error_code=exc.error_code, | |
| message=exc.detail, | |
| path=str(request.url.path), | |
| details=( | |
| { | |
| "request_id": request_id, | |
| "type": type(exc).__name__, | |
| "timestamp": datetime.utcnow().isoformat(), | |
| } | |
| if settings.DEBUG | |
| else None | |
| ), | |
| ) | |
| return JSONResponse( | |
| status_code=exc.status_code, | |
| content=jsonable_encoder( | |
| error_response.model_dump() | |
| ), | |
| ) | |
| # ========================================================= | |
| # VALIDATION EXCEPTION HANDLER | |
| # ========================================================= | |
| async def validation_exception_handler( | |
| request: Request, | |
| exc: Union[ | |
| RequestValidationError, | |
| ValidationError, | |
| ], | |
| ) -> JSONResponse: | |
| """ | |
| Handles validation errors | |
| """ | |
| request_id = getattr( | |
| request.state, | |
| "request_id", | |
| "unknown" | |
| ) | |
| logger.warning( | |
| f"VALIDATION_ERROR | " | |
| f"{request.method} " | |
| f"{request.url.path}" | |
| ) | |
| logger.debug( | |
| f"VALIDATION_DETAILS | " | |
| f"REQUEST_ID={request_id} | " | |
| f"ERRORS={exc.errors()}" | |
| ) | |
| errors = [] | |
| for error in exc.errors(): | |
| field = " -> ".join( | |
| str(loc) | |
| for loc in error.get("loc", []) | |
| if loc != "body" | |
| ) | |
| errors.append( | |
| ValidationErrorModel( | |
| field=field or "unknown", | |
| message=error.get( | |
| "msg", | |
| "Validation error", | |
| ), | |
| error_type=error.get( | |
| "type", | |
| "unknown_error", | |
| ), | |
| ) | |
| ) | |
| error_response = ValidationErrorResponse( | |
| errors=errors, | |
| message=( | |
| f"Validation failed for " | |
| f"{len(errors)} field(s)" | |
| ), | |
| ) | |
| return JSONResponse( | |
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | |
| content=jsonable_encoder( | |
| error_response.model_dump() | |
| ), | |
| ) | |
| # ========================================================= | |
| # GENERIC EXCEPTION HANDLER | |
| # ========================================================= | |
| async def generic_exception_handler( | |
| request: Request, | |
| exc: Exception, | |
| ) -> JSONResponse: | |
| """ | |
| Handles unhandled server exceptions | |
| """ | |
| request_id = getattr( | |
| request.state, | |
| "request_id", | |
| "unknown" | |
| ) | |
| logger.exception( | |
| f"UNHANDLED_EXCEPTION | " | |
| f"{request.method} " | |
| f"{request.url.path} | " | |
| f"REQUEST_ID={request_id}" | |
| ) | |
| error_response = ErrorResponse( | |
| error_code="INTERNAL_SERVER_ERROR", | |
| message=( | |
| str(exc) | |
| if settings.DEBUG | |
| else "An unexpected error occurred" | |
| ), | |
| path=str(request.url.path), | |
| details=( | |
| { | |
| "request_id": request_id, | |
| "type": type(exc).__name__, | |
| "timestamp": datetime.utcnow().isoformat(), | |
| } | |
| if settings.DEBUG | |
| else None | |
| ), | |
| ) | |
| return JSONResponse( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| content=jsonable_encoder( | |
| error_response.model_dump() | |
| ), | |
| ) | |
| # ========================================================= | |
| # STARTUP / SHUTDOWN HELPERS | |
| # ========================================================= | |
| def log_startup() -> None: | |
| """ | |
| Clean startup logging | |
| """ | |
| logger.info( | |
| f"Starting {settings.APP_NAME} " | |
| f"v{settings.APP_VERSION}" | |
| ) | |
| logger.info( | |
| f"Environment: " | |
| f"{settings.ENVIRONMENT}" | |
| ) | |
| logger.info( | |
| f"Debug mode: " | |
| f"{settings.DEBUG}" | |
| ) | |
| def log_shutdown() -> None: | |
| """ | |
| Clean shutdown logging | |
| """ | |
| logger.info( | |
| f"Shutting down {settings.APP_NAME}" | |
| ) |