refactor: Replace direct error logging with capture_and_log utility in performance and rate limiting middleware.

This commit is contained in:
pacnpal
2026-01-04 18:39:48 -05:00
parent bc4a3c7557
commit 89d9e945b9
3 changed files with 21 additions and 10 deletions

View File

@@ -9,6 +9,8 @@ from django.conf import settings
from django.db import connection from django.db import connection
from django.utils.deprecation import MiddlewareMixin from django.utils.deprecation import MiddlewareMixin
from apps.core.utils import capture_and_log
performance_logger = logging.getLogger("performance") performance_logger = logging.getLogger("performance")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -130,12 +132,11 @@ class PerformanceMiddleware(MiddlewareMixin):
), ),
} }
performance_logger.error( capture_and_log(
f"Request exception: {request.method} {request.path} - " exception,
f"{duration:.3f}s, {total_queries} queries, {type(exception).__name__}: { f'Request exception: {request.method} {request.path} - {duration:.3f}s, {total_queries} queries',
exception source='middleware',
}", severity='high',
extra=performance_data,
) )
# Don't return anything - let the exception propagate normally # Don't return anything - let the exception propagate normally

View File

@@ -19,6 +19,8 @@ from collections.abc import Callable
from django.core.cache import cache from django.core.cache import cache
from django.http import HttpRequest, HttpResponse, JsonResponse from django.http import HttpRequest, HttpResponse, JsonResponse
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -215,7 +217,9 @@ class SecurityEventLogger:
user = getattr(request, "user", None) user = getattr(request, "user", None)
username = user.username if user and user.is_authenticated else "anonymous" username = user.username if user and user.is_authenticated else "anonymous"
logger.error( capture_and_log(
f"Suspicious activity detected - Type: {activity_type}, " RuntimeError(f'Suspicious activity detected - Type: {activity_type}'),
f"IP: {client_ip}, User: {username}, Details: {details}" f'Suspicious activity - IP: {client_ip}, User: {username}, Details: {details}',
source='security',
severity='high',
) )

View File

@@ -20,6 +20,7 @@ from .signals import (
pre_state_transition, pre_state_transition,
state_transition_failed, state_transition_failed,
) )
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -84,7 +85,12 @@ def with_callbacks(
if not pre_success and pre_failures: if not pre_success and pre_failures:
for callback, exc in pre_failures: for callback, exc in pre_failures:
if not callback.continue_on_error: if not callback.continue_on_error:
logger.error(f"Pre-transition callback {callback.name} failed, " f"aborting transition") capture_and_log(
exc or RuntimeError(f'Pre-transition callback {callback.name} failed'),
f'Aborting transition - pre-callback failed',
source='state_machine',
severity='high',
)
if exc: if exc:
raise exc raise exc
raise RuntimeError(f"Pre-transition callback {callback.name} failed") raise RuntimeError(f"Pre-transition callback {callback.name} failed")