From d0dcf438e71a075c0717e5b9db5e9db96d61e8d5 Mon Sep 17 00:00:00 2001 From: gourav Date: Sun, 9 Aug 2026 08:37:03 +0530 Subject: [PATCH] Added logging --- compose.yaml | 3 ++- main.py | 56 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/compose.yaml b/compose.yaml index 50f15f1..69c2d37 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,4 +6,5 @@ services: env_file: - .env volumes: - - ./uploads:/uploads \ No newline at end of file + - ./uploads:/uploads + - ./logs:/logs \ No newline at end of file diff --git a/main.py b/main.py index 4908469..5491aab 100644 --- a/main.py +++ b/main.py @@ -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,10 +108,16 @@ 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)]) async def update_nav_ff(): @@ -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() \ No newline at end of file + 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"} \ No newline at end of file