Skip to content

Commit

Permalink
Merge pull request activist-org#908 from to-sta/custom_logger
Browse files Browse the repository at this point in the history
custom bad request error logs setup
  • Loading branch information
andrewtavis authored Jun 29, 2024
2 parents 288b872 + 3212eb2 commit 99b25e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions backend/backend/exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging
from typing import Any

from rest_framework.response import Response
from rest_framework.views import exception_handler

logger = logging.getLogger(__name__)


def bad_request_logger(exception: Any, context: dict[str, Any]) -> Response | None:
# Get the DRF exception handler standard error response
response = exception_handler(exception, context)

if response is not None:
logger.warning(
f"Bad request: {context['request'].path} -"
f"Status Code: {response.status_code} -"
f"Data: {response.data} -"
)

return response
1 change: 1 addition & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
"EXCEPTION_HANDLER": "backend.exception_handler.bad_request_logger",
}

SPECTACULAR_SETTINGS = {
Expand Down

0 comments on commit 99b25e9

Please sign in to comment.