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..6b0a634f --- /dev/null +++ b/src/therapy/log.py @@ -0,0 +1,36 @@ +"""Provide functions and utilities related to application logging.""" + +import logging + + +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: str | 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 = "therapy.log" + if quiet_upstream: + _quiet_upstream_libs() + logging.basicConfig( + filename=log_file, + format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s", + ) + logger = logging.getLogger(__package__) + logger.setLevel(log_level)