Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: properly configure logging in cli #447

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions src/therapy/log.py
Original file line number Diff line number Diff line change
@@ -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)
Loading