Skip to content

Commit

Permalink
feat: properly configure logging in cli (#447)
Browse files Browse the repository at this point in the history
Largely a copy/paste of work done in metakb. Small QOL increase for
running data updates.
  • Loading branch information
jsstevenson authored Dec 12, 2024
1 parent d8b6b68 commit 9690f95
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/therapy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DatabaseWriteError,
create_db,
)
from therapy.log import configure_logs
from therapy.schemas import SourceName

_logger = logging.getLogger(__name__)
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions src/therapy/log.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9690f95

Please sign in to comment.