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

@ -6,4 +6,5 @@ services:
env_file: env_file:
- .env - .env
volumes: volumes:
- ./uploads:/uploads - ./uploads:/uploads
- ./logs:/logs

56
main.py
View File

@ -1,15 +1,34 @@
import datetime
import logging
import os import os
from pathlib import Path
import bcrypt 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 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 from psycopg2.extras import execute_values
load_dotenv() 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() app = FastAPI()
UPLOAD_HASH = os.environ["UPLOAD_HASH"] UPLOAD_HASH = os.environ["UPLOAD_HASH"]
@ -66,8 +85,10 @@ async def test_db():
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute("select 1") cur.execute("select 1")
cur.fetchall() 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" return "If you're reading this, you're ready to update NAVs"
except Exception as e: except Exception as e:
logger.exception(f'test-db API was called and it failed with the following error: {e}')
return "No bueno" + e return "No bueno" + e
@ -87,10 +108,16 @@ async def update_nav(data: list[dict]):
for row in data for row in data
] ]
conn = connect_db() conn = connect_db()
with conn.cursor() as cursor: try:
execute_values(cursor, query, values) with conn.cursor() as cursor:
conn.commit() 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)]) @app.get('/update-navff', dependencies=[Depends(authenticate)])
async def update_nav_ff(): async def update_nav_ff():
@ -115,7 +142,12 @@ async def update_nav_ff():
nav_ff = EXCLUDED.nav_ff, nav_ff = EXCLUDED.nav_ff,
actual_date = EXCLUDED.actual_date; actual_date = EXCLUDED.actual_date;
""" """
try:
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute(update_query) cur.execute(update_query)
conn.commit() 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"}