Skip to content

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
FastestMolasses committed Nov 4, 2023
1 parent 1c20ce4 commit be9eb52
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/api/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from loguru import logger
from typing import Callable
from fastapi import Request
# from pyinstrument import Profiler
Expand All @@ -18,15 +19,18 @@ async def dispatch(self, request: Request, call_next: Callable):
response = await call_next(request)
return response

except NoResultFound:
except NoResultFound as e:
logger.exception(f'NoResultFound: {e}')
response = ServerResponse(status='error', message='Row not found')

except MultipleResultsFound:
except MultipleResultsFound as e:
logger.exception(f'MultipleResultsFound: {e}')
response = ServerResponse(
status='error', message='Multiple rows found')

except IntegrityError as e:
e.hide_parameters = True
logger.exception(f'IntegrityError: {e}')
response = ServerResponse(
status='error', message=str(e))

Expand All @@ -44,6 +48,7 @@ async def dispatch(self, request: Request, call_next: Callable):

except Exception as e:
# TODO: SEND NOTIFICATION HERE
logger.exception(e)
response = ServerResponse(
status='error', message=str(e))
return ORJSONResponse(response.dict(), status_code=400)
Expand Down
35 changes: 35 additions & 0 deletions app/log/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from loguru import logger


def setup_logging():
# Format of the logs
log_format = (
'<green>{time:YYYY-MM-DD HH:mm:ss!UTC}</green> | '
'<level>{level: <8}</level> | '
'<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - '
'<level>{message}</level>'
)

# General logs
logger.add(
'logs/server.log',
rotation='10 MB',
retention='30 days',
level='INFO', # INFO logs will log everything from INFO and above (WARNING, ERROR, etc.)
format=log_format,
enqueue=True,
filter=lambda record: not bool(record['exception']),
)

# Exception logs
logger.add(
'logs/exceptions.log',
rotation='10 MB',
retention='30 days',
level='ERROR', # ERROR will only log errors
format=log_format,
enqueue=True,
backtrace=True,
diagnose=True,
filter=lambda record: bool(record['exception']),
)
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi import FastAPI
from app.api.router import apiRouter
from app.core.config import settings
from app.log.setup import setup_logging
from app.types.server import ServerResponse
from fastapi.responses import ORJSONResponse
from starlette.middleware.cors import CORSMiddleware
Expand All @@ -19,6 +20,8 @@
)
server.include_router(apiRouter, prefix=settings.API_V1_STR)

setup_logging()

# Prometheus metrics
Instrumentator(
env_var_name='ENABLE_METRICS',
Expand Down

0 comments on commit be9eb52

Please sign in to comment.