Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger to seeder. #82

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/api/seeder/Dockerfile.seed
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN pip install --no-cache-dir -r seeder_reqs.txt
COPY main /app/api/
COPY models /app/api/
COPY seeder /app/api/
COPY logger_config.yaml /app/api/
WORKDIR /app

# Setup user to represent developer permissions in container
Expand Down
15 changes: 12 additions & 3 deletions app/api/seeder/seed_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from typing import List, Dict
import faker
from datetime import datetime, timedelta
import logging
import logging.config
import yaml

from sqlalchemy.orm import Session
from fastapi.testclient import TestClient
Expand All @@ -16,6 +19,13 @@

fake = faker.Faker()

# Load logger configuration
with open("api/logger_config.yaml", "r") as f:
log_cfg = yaml.safe_load(f)
logging.config.dictConfig(log_cfg)

logger = logging.getLogger(__name__)


def create_authed_client(email: str, password: str, client: TestClient):
"""Create an authenticated client for testing
Expand Down Expand Up @@ -83,7 +93,7 @@ def create_user(user: Dict, db: Session) -> User:
print(e)
db.rollback()
db.flush()
print(f"User {user_obj.email} already exists")
logger.error("Error adding user to database.", exc_info=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed to not specify the error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above that:

# Create user in database if it doesn't exist
user_obj = db.query(User).filter(User.email == user["email"]).first()
if user_obj:
print(f"User {user_obj.email} already exists")
return user_obj

It already checks if the user exists, so the error is probably something else that the stack trace will reveal using exc_info=True.

return user_obj


Expand Down Expand Up @@ -176,8 +186,7 @@ def seed_database():
print(e)
db.rollback()
db.flush()
print(f"ERROR creating patient encounter: {patient_encounter}")
print(f"\tERROR: {e}")
logger.error(f"Error adding patient encounter to db.", exc_info=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stack trace?


print(f"Created {counter} patient encounters for 2023 festival")

Expand Down
3 changes: 2 additions & 1 deletion app/api/seeder/seeder_reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ requests==2.25.1
SQLAlchemy==1.4.15
typing_extensions==4.2.0
SQLAlchemy-Utils==0.41.1
Faker==22.2.0
Faker==22.2.0
PyYAML==6.0
Loading