مراية لـ
https://github.com/Cesarkassis/booking-Api.git
تم المزامنة 2025-11-30 21:32:29 +00:00
Part 1: Booking API with Swagger and custom endpoints
هذا الالتزام موجود في:
41
bookings/middleware.py
Normal file
41
bookings/middleware.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# bookings/middleware.py
|
||||
import logging
|
||||
import time
|
||||
import json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class RequestLoggingMiddleware:
|
||||
"""
|
||||
Logs request start, method, path, body (if small), response status and duration.
|
||||
Place this middleware after common Django middleware as configured in settings.py.
|
||||
"""
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
start = time.time()
|
||||
try:
|
||||
# basic request info
|
||||
method = request.method
|
||||
path = request.get_full_path()
|
||||
# try to capture small JSON bodies safely
|
||||
body = None
|
||||
try:
|
||||
if method in ("POST", "PUT", "PATCH") and request.body:
|
||||
raw = request.body.decode("utf-8")[:2000]
|
||||
body = raw
|
||||
except Exception:
|
||||
body = "<could not decode>"
|
||||
|
||||
logger.info(f"Request start: {method} {path} body={body}")
|
||||
|
||||
response = self.get_response(request)
|
||||
|
||||
duration = time.time() - start
|
||||
logger.info(f"Request finished: {method} {path} status={response.status_code} time={duration:.3f}s")
|
||||
return response
|
||||
except Exception as ex:
|
||||
# log exception details
|
||||
logger.exception(f"Unhandled exception during request: {ex}")
|
||||
raise
|
||||
المرجع في مشكلة جديدة
حظر مستخدم