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.utils.deprecation import MiddlewareMixin
from apps.core.utils import capture_and_log
performance_logger = logging.getLogger("performance")
logger = logging.getLogger(__name__)
@@ -130,12 +132,11 @@ class PerformanceMiddleware(MiddlewareMixin):
),
}
performance_logger.error(
f"Request exception: {request.method} {request.path} - "
f"{duration:.3f}s, {total_queries} queries, {type(exception).__name__}: {
exception
}",
extra=performance_data,
capture_and_log(
exception,
f'Request exception: {request.method} {request.path} - {duration:.3f}s, {total_queries} queries',
source='middleware',
severity='high',
)
# 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.http import HttpRequest, HttpResponse, JsonResponse
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__)
@@ -215,7 +217,9 @@ class SecurityEventLogger:
user = getattr(request, "user", None)
username = user.username if user and user.is_authenticated else "anonymous"
logger.error(
f"Suspicious activity detected - Type: {activity_type}, "
f"IP: {client_ip}, User: {username}, Details: {details}"
capture_and_log(
RuntimeError(f'Suspicious activity detected - Type: {activity_type}'),
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,
state_transition_failed,
)
from apps.core.utils import capture_and_log
logger = logging.getLogger(__name__)
@@ -84,7 +85,12 @@ def with_callbacks(
if not pre_success and pre_failures:
for callback, exc in pre_failures:
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:
raise exc
raise RuntimeError(f"Pre-transition callback {callback.name} failed")