mirror of
https://github.com/aykhans/portfolio-blog.git
synced 2025-04-15 02:23:12 +00:00
Added logging with mongodb
This commit is contained in:
parent
39bc03cb68
commit
e5f5624635
@ -72,7 +72,9 @@ class Settings(BaseSettings):
|
||||
|
||||
MONGO_INITDB_ROOT_USERNAME: str
|
||||
MONGO_INITDB_ROOT_PASSWORD: str
|
||||
MONGO_DB_PORT: int = 27017
|
||||
MONGO_DB_LOGS_NAME: str = 'logs'
|
||||
MONGO_DB_DEFAULT_COLLECTION: str = 'init'
|
||||
|
||||
@property
|
||||
def MONGO_DB_DSN(self) -> MongoDsn:
|
||||
@ -81,7 +83,7 @@ class Settings(BaseSettings):
|
||||
username=self.MONGO_INITDB_ROOT_USERNAME,
|
||||
password=self.MONGO_INITDB_ROOT_PASSWORD,
|
||||
host='mongodb',
|
||||
port=27017,
|
||||
port=self.MONGO_DB_PORT,
|
||||
)
|
||||
|
||||
# ----------------------------- MongoDB End -----------------------------
|
||||
|
@ -36,7 +36,7 @@ app.include_router(main_router)
|
||||
|
||||
@app.on_event('startup')
|
||||
async def app_startup():
|
||||
await create_mongodb_database_if_not_exists('logs')
|
||||
await create_mongodb_database_if_not_exists(settings.MONGO_DB_LOGS_NAME)
|
||||
|
||||
# -------------------------------- Events end --------------------------------
|
||||
|
||||
|
13
src/app/schemas/logs.py
Normal file
13
src/app/schemas/logs.py
Normal file
@ -0,0 +1,13 @@
|
||||
from pydantic import BaseModel, PastDatetime
|
||||
|
||||
|
||||
class MongoDBLog(BaseModel):
|
||||
created: PastDatetime
|
||||
args: dict = {}
|
||||
filename: str
|
||||
levelname: str
|
||||
func_name: str
|
||||
lineno: int
|
||||
msg: str
|
||||
name: str
|
||||
pathname: str
|
@ -1,5 +1,15 @@
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime
|
||||
from logging import (
|
||||
Handler,
|
||||
LogRecord
|
||||
)
|
||||
|
||||
from pymongo.database import Database
|
||||
from pymongo.collection import Collection
|
||||
|
||||
from app.core.config import settings
|
||||
from app.schemas.logs import MongoDBLog
|
||||
from app.views.depends import get_mongo_client
|
||||
|
||||
|
||||
@ -9,4 +19,25 @@ async def create_mongodb_database_if_not_exists(
|
||||
|
||||
with contextmanager(get_mongo_client)() as client:
|
||||
if db_name not in client.list_database_names():
|
||||
client[db_name].create_collection('init')
|
||||
client[db_name].\
|
||||
create_collection(settings.MONGO_DB_DEFAULT_COLLECTION)
|
||||
|
||||
|
||||
class MongoDBLogHandler(Handler):
|
||||
def emit(self, record: LogRecord) -> None:
|
||||
with contextmanager(get_mongo_client)() as client:
|
||||
db: Database = client[settings.MONGO_DB_LOGS_NAME]
|
||||
collection: Collection = db['init']
|
||||
collection.insert_one(
|
||||
MongoDBLog(
|
||||
created=datetime.fromtimestamp(record.created),
|
||||
filename=record.filename,
|
||||
levelname=record.levelname,
|
||||
func_name=record.funcName,
|
||||
lineno=record.lineno,
|
||||
msg=record.getMessage(),
|
||||
name=record.name,
|
||||
pathname=record.pathname,
|
||||
args=record.args if record.args else {}
|
||||
).model_dump()
|
||||
)
|
@ -1,4 +1,5 @@
|
||||
from typing import Annotated
|
||||
import logging
|
||||
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import (
|
||||
@ -20,8 +21,10 @@ from app.core.config import settings
|
||||
from app.schemas import ListPostInTemplate
|
||||
from app.schemas.main import SendEmail
|
||||
from app.schemas.post import PostDetail
|
||||
from app.utils.custom_functions import get_remote_address
|
||||
from app.utils.email_utils import send_email_notification
|
||||
from app.models.user import User as UserModel
|
||||
from app.utils.mongodb_utils import MongoDBLogHandler
|
||||
from app.utils.rate_limiter import limiter
|
||||
from app.views.depends import (
|
||||
get_async_db,
|
||||
@ -33,6 +36,10 @@ router = APIRouter()
|
||||
|
||||
templates = Jinja2Templates(directory=settings.APP_PATH / 'templates')
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.INFO)
|
||||
mongo_db_log_handler = MongoDBLogHandler()
|
||||
logger.addHandler(mongo_db_log_handler)
|
||||
|
||||
@router.get('/', response_class=HTMLResponse)
|
||||
async def home(request: Request):
|
||||
@ -43,7 +50,7 @@ async def home(request: Request):
|
||||
|
||||
|
||||
@router.post('/send-email')
|
||||
@limiter.limit('2/day')
|
||||
@limiter.limit('222/day')
|
||||
async def send_email(
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
@ -55,6 +62,8 @@ async def send_email(
|
||||
f"phone: {form_data.phone}\n"\
|
||||
f"message: {form_data.message}"
|
||||
|
||||
logger.info(body, {'client': get_remote_address(request)})
|
||||
|
||||
email_notification = send_email_notification(
|
||||
subject=f"Portfolio Blog (by {form_data.email})",
|
||||
body=body
|
||||
|
Loading…
x
Reference in New Issue
Block a user