From 4fa4b10cfe6a603ce966fdd88f3e563a1acd749f Mon Sep 17 00:00:00 2001 From: lampham789 Date: Wed, 28 Aug 2024 17:33:16 -0700 Subject: [PATCH] Enhance logging for config.py and tweaked conftest.py --- app/config.py | 25 +++++++++++++++++++++++-- tests/conftest.py | 8 +++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/config.py b/app/config.py index 5b724b4..0e97911 100644 --- a/app/config.py +++ b/app/config.py @@ -1,10 +1,20 @@ import os import json +import logging import boto3 from botocore.exceptions import ClientError +logging.basicConfig( + level=logging.INFO, + format="[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S %z" +) + +logger = logging.getLogger(__name__) + + def get_secret(secret_name): client = boto3.client("secretsmanager") @@ -13,6 +23,7 @@ def get_secret(secret_name): SecretId=secret_name ) except ClientError as e: + logger.error(f"Failed to retrieve secret.") raise e secret = get_secret_value_response["SecretString"] @@ -22,26 +33,36 @@ def get_secret(secret_name): def get_database_uri(): database_uri = os.getenv("DATABASE_URI") if database_uri: + logger.info("Using DATABASE_URI from environment.") return database_uri db_credentials_secret_name = os.getenv("DATABASE_CREDENTIALS_SECRET_NAME") if db_credentials_secret_name: + logger.info(f"Retrieving database credentials from Secrets Manager") secret = get_secret(db_credentials_secret_name) db_username = secret["username"] db_password = secret["password"] db_host = secret["host"] db_database = secret["dbname"] else: + logger.info("Retrieving database credentials from environment variables.") db_username = os.getenv("DB_USERNAME") db_password = os.getenv("DB_PASSWORD") db_host = os.getenv("DB_HOST") db_database = os.getenv("DB_DATABASE") + if not all([db_username, db_password, db_host, db_database]): + logger.info("Database credentials are not fully specified, override with test database URI.") + return "" # Override in tests. + return f"mysql+mysqlconnector://{db_username}:{db_password}@{db_host}/{db_database}?charset=utf8mb4" class Config: - TESTING = False LOG_LEVEL = os.getenv("LOG_LEVEL") SQLALCHEMY_DATABASE_URI = get_database_uri() - SQLALCHEMY_TRACK_MODIFICATIONS = False \ No newline at end of file + SQLALCHEMY_TRACK_MODIFICATIONS = False + SQLALCHEMY_ENGINE_OPTIONS = { + "pool_pre_ping": True, + "echo": os.getenv("FLASK_ENV") != "production" + } \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index d9ee644..0b4fa53 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,10 @@ import os -import pytest import shutil +import pytest from app import create_app + @pytest.fixture def td_tmpdir(request, tmpdir): """Copy test data into the temporary directory for tests, if available. @@ -55,7 +56,4 @@ class TestConfig: @pytest.fixture def client(app): - client = app.test_client() - yield client - - + return app.test_client() \ No newline at end of file