Skip to content

Commit 4fa4b10

Browse files
committed
Enhance logging for config.py and tweaked conftest.py
1 parent 130c178 commit 4fa4b10

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

app/config.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import os
22
import json
3+
import logging
34

45
import boto3
56
from botocore.exceptions import ClientError
67

78

9+
logging.basicConfig(
10+
level=logging.INFO,
11+
format="[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s",
12+
datefmt="%Y-%m-%d %H:%M:%S %z"
13+
)
14+
15+
logger = logging.getLogger(__name__)
16+
17+
818
def get_secret(secret_name):
919
client = boto3.client("secretsmanager")
1020

@@ -13,6 +23,7 @@ def get_secret(secret_name):
1323
SecretId=secret_name
1424
)
1525
except ClientError as e:
26+
logger.error(f"Failed to retrieve secret.")
1627
raise e
1728

1829
secret = get_secret_value_response["SecretString"]
@@ -22,26 +33,36 @@ def get_secret(secret_name):
2233
def get_database_uri():
2334
database_uri = os.getenv("DATABASE_URI")
2435
if database_uri:
36+
logger.info("Using DATABASE_URI from environment.")
2537
return database_uri
2638

2739
db_credentials_secret_name = os.getenv("DATABASE_CREDENTIALS_SECRET_NAME")
2840
if db_credentials_secret_name:
41+
logger.info(f"Retrieving database credentials from Secrets Manager")
2942
secret = get_secret(db_credentials_secret_name)
3043
db_username = secret["username"]
3144
db_password = secret["password"]
3245
db_host = secret["host"]
3346
db_database = secret["dbname"]
3447
else:
48+
logger.info("Retrieving database credentials from environment variables.")
3549
db_username = os.getenv("DB_USERNAME")
3650
db_password = os.getenv("DB_PASSWORD")
3751
db_host = os.getenv("DB_HOST")
3852
db_database = os.getenv("DB_DATABASE")
3953

54+
if not all([db_username, db_password, db_host, db_database]):
55+
logger.info("Database credentials are not fully specified, override with test database URI.")
56+
return "" # Override in tests.
57+
4058
return f"mysql+mysqlconnector://{db_username}:{db_password}@{db_host}/{db_database}?charset=utf8mb4"
4159

4260

4361
class Config:
44-
TESTING = False
4562
LOG_LEVEL = os.getenv("LOG_LEVEL")
4663
SQLALCHEMY_DATABASE_URI = get_database_uri()
47-
SQLALCHEMY_TRACK_MODIFICATIONS = False
64+
SQLALCHEMY_TRACK_MODIFICATIONS = False
65+
SQLALCHEMY_ENGINE_OPTIONS = {
66+
"pool_pre_ping": True,
67+
"echo": os.getenv("FLASK_ENV") != "production"
68+
}

tests/conftest.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os
2-
import pytest
32
import shutil
43

4+
import pytest
55
from app import create_app
66

7+
78
@pytest.fixture
89
def td_tmpdir(request, tmpdir):
910
"""Copy test data into the temporary directory for tests, if available.
@@ -55,7 +56,4 @@ class TestConfig:
5556

5657
@pytest.fixture
5758
def client(app):
58-
client = app.test_client()
59-
yield client
60-
61-
59+
return app.test_client()

0 commit comments

Comments
 (0)