Skip to content

Commit

Permalink
feat: Add logging mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowtham1729 committed Dec 10, 2023
1 parent b95552f commit 78e1387
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions applications/data_fetcher/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

WORKDIR /app
COPY fetcher.py /app
COPY utils/ /app/utils

EXPOSE 8000

Expand Down
17 changes: 11 additions & 6 deletions applications/data_fetcher/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import os
from dataclasses import dataclass
from datetime import datetime
from typing import Dict, List

import psycopg
import requests
from utils.logging import get_logger

logger = get_logger(__name__)

DB_HOST = os.getenv("DB_HOST", "localhost")
DB_PORT = os.getenv("DB_PORT", "5432")
Expand Down Expand Up @@ -98,17 +100,17 @@ def to_news(self, items: Dict, tickers_list: List[str]) -> List[News]:
return news

def fetch_news(self):
logging.info(f"Fetching Tickers...")
logger.info(f"Fetching Tickers...")
tickers = self.get_tickers()
tickers_list = [ticker.ticker for ticker in tickers]
logging.info(f"Ticker List: {tickers_list}")
logger.info(f"Ticker List: {tickers_list}")

ticker_symbols = ",".join(tickers_list)
today_date = datetime.today().strftime("%Y-%m-%d")
page = 1
news_url = f"{self.marketaux_news_url}&symbols={ticker_symbols}&published_on={today_date}"

logging.info(f"Fetching News...")
logger.info(f"Fetching News...")
response = requests.get(f"{news_url}&page={page}")
news = []
if response.status_code == 200:
Expand All @@ -127,11 +129,14 @@ def fetch_news(self):
news += self.to_news(response_json["data"], tickers_list)
else:
break
logging.info(f"Finished fetching News: {news}")
logging.info(f"Inserting News...")
logger.info(f"Finished fetching News: {news}")
logger.info(f"Inserting News...")
self.insert_news(news)


if __name__ == "__main__":
logger.info(f"Starting Fetcher...")
fetcher = Fetcher()
logger.info(f"Fetcher Started...")
fetcher.fetch_news()
logger.info(f"Fetcher Finished...")
13 changes: 12 additions & 1 deletion applications/data_fetcher/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/data_fetcher/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ requests = "^2.31.0"
psycopg = {extras = ["binary", "pool"], version = "^3.1.12"}
pika = "^1.3.2"
fire = "^0.5.0"
python-json-logger = "^2.0.7"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions applications/data_fetcher/utils/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging

from pythonjsonlogger import jsonlogger


def get_logger(name=None, level=logging.INFO, log_file=None):
logger = logging.getLogger(name)
logger.setLevel(level)

# Specify the log record attributes to include in the logs
formatter = jsonlogger.JsonFormatter("%(asctime)s %(levelname)s %(message)s")

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

return logger

0 comments on commit 78e1387

Please sign in to comment.