Added logging

This commit is contained in:
Gourav Kumar 2026-08-09 08:37:03 +05:30
parent 689ea57bbe
commit d0dcf438e7
2 changed files with 46 additions and 13 deletions

View File

@ -7,3 +7,4 @@ services:
- .env
volumes:
- ./uploads:/uploads
- ./logs:/logs

54
main.py
View File

@ -1,15 +1,34 @@
import datetime
import logging
import os
from pathlib import Path
import bcrypt
from fastapi import Depends, FastAPI, HTTPException, UploadFile, Response
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from dotenv import load_dotenv
from pathlib import Path
import psycopg2
from dotenv import load_dotenv
from fastapi import Depends, FastAPI, HTTPException, Response, UploadFile
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from psycopg2.extras import execute_values
load_dotenv()
LOG_DIR = Path("/logs")
LOG_DIR.mkdir(parents=True, exist_ok=True)
logger = logging.getLogger("file_uploader")
logger.setLevel(logging.INFO)
log_file = LOG_DIR / f"{datetime.datetime.now():%Y-%m}.log"
handler = logging.FileHandler(log_file)
handler.setFormatter(
logging.Formatter(
"%(asctime)s | %(levelname)s | %(message)s"
)
)
logger.addHandler(handler)
app = FastAPI()
UPLOAD_HASH = os.environ["UPLOAD_HASH"]
@ -66,8 +85,10 @@ async def test_db():
with conn.cursor() as cur:
cur.execute("select 1")
cur.fetchall()
logger.info('test-db API was called and it was successful')
return "If you're reading this, you're ready to update NAVs"
except Exception as e:
logger.exception(f'test-db API was called and it failed with the following error: {e}')
return "No bueno" + e
@ -87,9 +108,15 @@ async def update_nav(data: list[dict]):
for row in data
]
conn = connect_db()
with conn.cursor() as cursor:
execute_values(cursor, query, values)
conn.commit()
try:
with conn.cursor() as cursor:
execute_values(cursor, query, values)
conn.commit()
logger.info('Update NAV was called and db update succeeded')
return {'message': "Database updated succesfully"}
except Exception as e:
logger.exception(f"Update Db errored out: {e}")
return {'message': "database update failed. Check logs for details"}
@app.get('/update-navff', dependencies=[Depends(authenticate)])
@ -115,7 +142,12 @@ async def update_nav_ff():
nav_ff = EXCLUDED.nav_ff,
actual_date = EXCLUDED.actual_date;
"""
with conn.cursor() as cur:
cur.execute(update_query)
conn.commit()
try:
with conn.cursor() as cur:
cur.execute(update_query)
conn.commit()
logger.info('Update NAV FF was called and db update succeeded')
return {'message': "Database updated succesfully"}
except Exception as e:
logger.exception(f"Update NAV FF errored out: {e}")
return {'message': "database update failed. Check logs for details"}