Skip to content

Commit

Permalink
feat: implement a better logging system for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sali72 committed Dec 11, 2024
1 parent 54675af commit 15367e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
38 changes: 36 additions & 2 deletions commons/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from fastapi.responses import JSONResponse
import logging
import traceback

from fastapi import HTTPException
from fastapi.responses import JSONResponse
from commons.logging_config import setup_logging

setup_logging()
logger = logging.getLogger(__name__)


exception_status_map = {
# Built-in Python exception
Expand All @@ -13,17 +21,42 @@
"InvalidId": 400,
}


def get_exception_name(exc: Exception) -> str:
"""Get the name of the exception."""
return type(exc).__name__


def get_status_code(exception_name: str) -> int:
"""Get the status code based on the exception name."""
return exception_status_map.get(exception_name, 500)


def extract_traceback_info(exc: Exception):
"""Extract file name and line number from the exception traceback."""
tb = traceback.extract_tb(exc.__traceback__)
if tb:
last_trace = tb[-1]
return last_trace.filename, last_trace.lineno
return "Unknown", "Unknown"


def log_exception(exc: Exception, file_name: str, line_number: int):
"""Log the full exception information."""
logging.error(
"Exception occurred: %s\nFile: %s, Line: %d\nTraceback: %s",
str(exc),
file_name,
line_number,
"".join(traceback.format_exception(type(exc), exc, exc.__traceback__)),
)


async def base_exception_handler(request, exc: Exception):
exception_name = get_exception_name(exc)
status_code = get_status_code(exception_name)
file_name, line_number = extract_traceback_info(exc)
log_exception(exc, file_name, line_number)

return JSONResponse(
status_code=status_code,
Expand All @@ -33,11 +66,12 @@ async def base_exception_handler(request, exc: Exception):
},
)


async def http_exception_handler(request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
"exception_name": "HTTPException",
"detail": exc.detail,
},
)
)
7 changes: 7 additions & 0 deletions commons/logging_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import logging

def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
4 changes: 3 additions & 1 deletion database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dotenv import load_dotenv
from mongoengine.connection import get_connection

from commons.logging_config import setup_logging
from database.initialize_db import (
initialize_common_asset_types,
initialize_common_categories,
Expand All @@ -21,7 +22,8 @@
MONGO_ATLAS_CONNECTION_STRING = os.getenv("MONGO_ATLAS_CONNECTION_STRING")
TEST_MODE = os.getenv("TEST_MODE")

logging.basicConfig(level=logging.INFO)

setup_logging()
logger = logging.getLogger(__name__)


Expand Down

0 comments on commit 15367e9

Please sign in to comment.