29 lines
1000 B
Python
29 lines
1000 B
Python
"""
|
|
Request logging middleware - logs every HTTP request to EventifyLogger.
|
|
"""
|
|
from eventify_logger.services import log
|
|
|
|
|
|
class EventifyLoggingMiddleware:
|
|
"""Log each request (method, path, status) after response."""
|
|
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
|
|
def __call__(self, request):
|
|
response = self.get_response(request)
|
|
try:
|
|
status = getattr(response, "status_code", 0)
|
|
if 500 <= status < 600:
|
|
logger_type = "error"
|
|
elif 400 <= status < 500:
|
|
logger_type = "warning"
|
|
else:
|
|
logger_type = "info"
|
|
message = f"{request.method} {request.path} -> {status}"
|
|
logger_data = {"path": request.path, "method": request.method, "status_code": status}
|
|
log(logger_type=logger_type, logger_message=message, request=request, logger_data=logger_data)
|
|
except Exception:
|
|
pass
|
|
return response
|