Skip to content

Commit

Permalink
Do not print info on every visit and visit persistence; set log level…
Browse files Browse the repository at this point in the history
… to debug only if ENVIRONMENT=DEV
  • Loading branch information
vladsavelyev committed Feb 23, 2024
1 parent 2ce0f17 commit 21c3723
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
tmp_path = Path(os.getenv("TMPDIR", "/tmp"))
log_path = tmp_path / "multiqc_api.log"
logging.basicConfig(
level=logging.DEBUG,
level=logging.DEBUG if os.getenv("ENVIRONMENT") == "DEV" else logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(),
Expand Down
11 changes: 4 additions & 7 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _log_visit(
"is_ci": is_ci,
}
)
logger.info(f"Logging visit, total visits: {len(visit_buffer)}")
logger.debug(f"Logging visit, total visits: {len(visit_buffer)}")


# Path to a buffer CSV file to persist recent visits before dumping to the database
Expand All @@ -161,22 +161,19 @@ def _persist_visits() -> Response:
with open(CSV_FILE_PATH, mode="r") as file:
n_visits_file = sum(1 for _ in file)
if not visit_buffer:
msg = f"No new visits to persist. File contains {n_visits_file} entries"
logger.info(msg)
return PlainTextResponse(content=msg)
logger.info(
return PlainTextResponse(content=f"No new visits to persist. File contains {n_visits_file} entries")
logger.debug(
f"Appending {len(visit_buffer)} visits to {CSV_FILE_PATH} that currently contains {n_visits_file} visits"
)
with open(CSV_FILE_PATH, mode="a") as file:
writer: csv.DictWriter = csv.DictWriter(file, fieldnames=["timestamp"] + visit_fieldnames)
writer.writerows(visit_buffer)

logger.info(f"Persisted {len(visit_buffer)} visits to CSV {CSV_FILE_PATH}")
visit_buffer = []
with open(CSV_FILE_PATH, mode="r") as file:
n_visits_file = sum(1 for _ in file)
msg = f"Successfully persisted {len(visit_buffer)} visits to {CSV_FILE_PATH}, file now contains {n_visits_file} entries"
logger.info(msg)
logger.debug(msg)
return PlainTextResponse(content=msg)


Expand Down

0 comments on commit 21c3723

Please sign in to comment.