Skip to content

Commit

Permalink
chore: modify logging sink to just print
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Oct 30, 2023
1 parent 57fb389 commit 4843547
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
15 changes: 7 additions & 8 deletions icon_stats/crons/cmc_cryptocurrency_quotes_latest.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
from icon_stats.log import logger
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError

from icon_stats.utils.times import convert_str_date
from icon_stats.clients.cmc import new_cmc_client
from icon_stats.db import upsert_model
from icon_stats.metrics import prom_metrics
from icon_stats.models.cmc_cryptocurrency_quotes_latest import CmcListingsLatestQuote
from icon_stats.clients.cmc import new_cmc_client
from icon_stats.db import get_session, upsert_model
from icon_stats.utils.times import convert_str_date


async def run_cmc_cryptocurrency_quotes_latest():
"""
This cron scrapes the cmc endpoint for
"""
logger.info("Starting top tokens cron")
logger.info(f"Starting {__name__} cron")

cmc_client = new_cmc_client()
response = await cmc_client.cryptocurrency_quotes_latest(symbol='ICX')

if isinstance(response, dict) and response['status']['error_code'] == 0:
quote = response['data']['ICX']['quote']['USD']
else:
logger.info("Ending top tokens cron")
logger.info(f"Ending {__name__} cron because bad response - "
f"{response['status']['error_code']}")
return

quote['last_updated'] = convert_str_date(quote['last_updated'])
exchanges_legacy = CmcListingsLatestQuote(base='ICX', quote='USD', **quote)

try:
await upsert_model(db_name='stats', model=exchanges_legacy)
except IntegrityError:
logger.info("Duplicate PK - skipping upsert")

prom_metrics.cron_ran.inc()
logger.info("Ending cmc crypto quotes latest cron")
logger.info(f"Ending {__name__} cron")
43 changes: 24 additions & 19 deletions icon_stats/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
from icon_stats.config import config


def sink(message):
try:
record = message.record
log_data = {
"timestamp": record["time"].strftime('%Y-%m-%d %H:%M:%S'),
}

if "structured" in record["extra"]:
log_data["data"] = record["extra"]["structured"]
else:
log_data["message"] = record["message"]

if config.LOG_FORMAT == "json":
return json.dumps(log_data, indent=config.STRUCTURED_SETTINGS.INDENT)
else:
return log_data["message"]
except Exception as e:
pass
async def sink(message):
record = message.record
log_data = {
"timestamp": record["time"].strftime('%Y-%m-%d %H:%M:%S'),
}

if "structured" in record["extra"]:
log_data["data"] = record["extra"]["structured"]
else:
log_data["message"] = record["message"]

if config.LOG_FORMAT == "json":
return json.dumps(log_data, indent=config.STRUCTURED_SETTINGS.INDENT)
else:
print(log_data["message"])
return log_data["message"]

logger.remove()
logger.add(sink, level=config.LOG_LEVEL, enqueue=True)
# logger.add(sink, level=config.LOG_LEVEL, enqueue=True)
logger.add(sink, level=config.LOG_LEVEL)

# import sys
# logger.remove()
# logger.add(sys.stdout, level=config.LOG_LEVEL)
# logger.info("Test log")


# If logging to a file
if config.LOG_TO_FILE:
Expand Down

0 comments on commit 4843547

Please sign in to comment.