Spaces:
Runtime error
Runtime error
File size: 10,031 Bytes
2908872 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | # 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}"
) |