From f7b98ac9c96bc4ec3bb19fb6f250a7e569051fbe Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Thu, 12 Dec 2024 14:06:54 -0500 Subject: [PATCH 1/2] feat: properly configure logging in cli --- src/therapy/cli.py | 3 +++ src/therapy/log.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/therapy/log.py diff --git a/src/therapy/cli.py b/src/therapy/cli.py index bf4a88d9..148044c6 100644 --- a/src/therapy/cli.py +++ b/src/therapy/cli.py @@ -16,6 +16,7 @@ DatabaseWriteError, create_db, ) +from therapy.log import configure_logs from therapy.schemas import SourceName _logger = logging.getLogger(__name__) @@ -32,6 +33,7 @@ def check_db(db_url: str, verbose: bool = False) -> None: :param db_url: URL to normalizer database :param verbose: if true, print result to console """ # noqa: D301 + configure_logs() db = create_db(db_url, False) if not db.check_schema_initialized(): if verbose: @@ -237,6 +239,7 @@ def update_normalizer_db( :param update_merged: if true, update normalized records :param use_existing: if True, use most recent local data instead of fetching latest version """ # noqa: D301 + configure_logs() db = create_db(db_url, aws_instance) if update_all: diff --git a/src/therapy/log.py b/src/therapy/log.py new file mode 100644 index 00000000..662650d6 --- /dev/null +++ b/src/therapy/log.py @@ -0,0 +1,37 @@ +"""Provide functions and utilities related to application logging.""" + +import logging +from pathlib import Path + + +def _quiet_upstream_libs() -> None: + """Turn off debug logging for chatty upstream library loggers.""" + for lib in ( + "boto3", + "botocore", + "urllib3", + ): + logging.getLogger(lib).setLevel(logging.INFO) + + +def configure_logs( + log_file: Path | None = None, + log_level: int = logging.DEBUG, + quiet_upstream: bool = True, +) -> None: + """Configure logging. + + :param log_filename: location to put log file at + :param log_level: global log level to set + :param quiet_upstream: if True, turn off debug logging for a selection of libraries + """ + if log_file is None: + log_file = Path("therapy.log") + if quiet_upstream: + _quiet_upstream_libs() + logging.basicConfig( + filename=log_file.absolute().as_uri(), + format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s", + ) + logger = logging.getLogger(__package__) + logger.setLevel(log_level) From 4647e8b51b821d5de4aa9a42fed865836e6f25ce Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Thu, 12 Dec 2024 14:28:01 -0500 Subject: [PATCH 2/2] fix path --- src/therapy/log.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/therapy/log.py b/src/therapy/log.py index 662650d6..6b0a634f 100644 --- a/src/therapy/log.py +++ b/src/therapy/log.py @@ -1,7 +1,6 @@ """Provide functions and utilities related to application logging.""" import logging -from pathlib import Path def _quiet_upstream_libs() -> None: @@ -15,7 +14,7 @@ def _quiet_upstream_libs() -> None: def configure_logs( - log_file: Path | None = None, + log_file: str | None = None, log_level: int = logging.DEBUG, quiet_upstream: bool = True, ) -> None: @@ -26,11 +25,11 @@ def configure_logs( :param quiet_upstream: if True, turn off debug logging for a selection of libraries """ if log_file is None: - log_file = Path("therapy.log") + log_file = "therapy.log" if quiet_upstream: _quiet_upstream_libs() logging.basicConfig( - filename=log_file.absolute().as_uri(), + filename=log_file, format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s", ) logger = logging.getLogger(__package__)