From 6fccf7db3117fbd94bf83ffe23fcba900967e639 Mon Sep 17 00:00:00 2001 From: Doug Lazenby Date: Wed, 26 Aug 2026 17:36:58 +0100 Subject: [PATCH] refactor(logging): replace package print statements with module-level loggers --- src/update_tf_modules/clients/github_api.py | 16 +++++++++------- src/update_tf_modules/clients/registry_api.py | 12 ++++++++---- src/update_tf_modules/discovery.py | 10 ++++++++-- src/update_tf_modules/updaters/github_source.py | 6 +++++- .../updaters/registry_source.py | 6 +++++- tests/test_discovery.py | 10 +++++----- 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/update_tf_modules/clients/github_api.py b/src/update_tf_modules/clients/github_api.py index 8c0d5e3..f5351f1 100644 --- a/src/update_tf_modules/clients/github_api.py +++ b/src/update_tf_modules/clients/github_api.py @@ -1,10 +1,14 @@ import os +import logging import requests from ..config import GITHUB_API +logger = logging.getLogger(__name__) + + def build_github_session() -> requests.Session: """Create an HTTP session configured for GitHub API requests. @@ -67,10 +71,10 @@ def get_latest_github_tag( raise ValueError(f"Unsupported GitHub lookup strategy: {lookup}") except requests.HTTPError as error: - print(f"[ERROR] Failed to fetch latest GitHub version for '{repo}': {error}") + logger.error(f"Failed to fetch latest GitHub version for '{repo}': {error}") return None except Exception as error: - print(f"[ERROR] Unexpected error fetching GitHub version for '{repo}': {error}") + logger.exception(f"Unexpected error fetching GitHub version for '{repo}': {error}") return None def get_commit_hash_for_tag( @@ -99,12 +103,10 @@ def get_commit_hash_for_tag( return tag_response.json().get("object", {}).get("sha") return obj.get("sha") except requests.HTTPError as error: - print( - f"[ERROR] Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}" - ) + logger.error(f"Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}") return None except Exception as error: - print( - f"[ERROR] Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}" + logger.exception( + f"Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}" ) return None diff --git a/src/update_tf_modules/clients/registry_api.py b/src/update_tf_modules/clients/registry_api.py index fba5afa..5e72e9d 100644 --- a/src/update_tf_modules/clients/registry_api.py +++ b/src/update_tf_modules/clients/registry_api.py @@ -1,9 +1,13 @@ import re +import logging import requests from ..config import TERRAFORM_REGISTRY_API + +logger = logging.getLogger(__name__) + def build_registry_session() -> requests.Session: """Create an HTTP session configured for Terraform Registry requests. @@ -43,13 +47,13 @@ def get_latest_registry_version( return None return max(version_numbers, key=semver_key) except requests.HTTPError as error: - print( - f"[ERROR] Failed to fetch latest version for registry module '{source}': {error}" + logger.error( + f"Failed to fetch latest version for registry module '{source}': {error}" ) return None except Exception as error: - print( - f"[ERROR] Unexpected error fetching version for registry module '{source}': {error}" + logger.exception( + f"Unexpected error fetching version for registry module '{source}': {error}" ) return None diff --git a/src/update_tf_modules/discovery.py b/src/update_tf_modules/discovery.py index bf3046e..be8e35e 100644 --- a/src/update_tf_modules/discovery.py +++ b/src/update_tf_modules/discovery.py @@ -1,8 +1,12 @@ import re +import logging from .config import TERRAFORM_ROOT from .models import GitHubModule, Module + +logger = logging.getLogger(__name__) + def normalize_discovered_source(source: str) -> str: """Normalize discovered module sources for manifest key comparison. @@ -81,6 +85,8 @@ def warn_on_unmanaged_modules(modules: list[Module]) -> None: unmanaged = sorted(discovered - managed) if unmanaged: - print("[WARN] Terraform modules were found in the repo but are not represented in the manifest:") + logger.warning( + "Terraform modules were found in the repo but are not represented in the manifest:" + ) for source in unmanaged: - print(f" - {source}") + logger.warning(f" - {source}") diff --git a/src/update_tf_modules/updaters/github_source.py b/src/update_tf_modules/updaters/github_source.py index bccee24..2645915 100644 --- a/src/update_tf_modules/updaters/github_source.py +++ b/src/update_tf_modules/updaters/github_source.py @@ -1,8 +1,12 @@ from pathlib import Path import re +import logging from ..config import ROOT + +logger = logging.getLogger(__name__) + def update_github_module( file_path: Path, source_prefix: str, @@ -38,6 +42,6 @@ def replace(match: re.Match[str]) -> str: if replacements > 0: file_path.write_text(new_content, encoding="utf-8") - print(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}") + logger.info(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}") return replacements \ No newline at end of file diff --git a/src/update_tf_modules/updaters/registry_source.py b/src/update_tf_modules/updaters/registry_source.py index 612f4f9..ea2eb02 100644 --- a/src/update_tf_modules/updaters/registry_source.py +++ b/src/update_tf_modules/updaters/registry_source.py @@ -1,8 +1,12 @@ from pathlib import Path import re +import logging from ..config import ROOT + +logger = logging.getLogger(__name__) + def update_registry_module( file_path: Path, source: str, @@ -90,7 +94,7 @@ def update_registry_module( old_content = file_path.read_text(encoding="utf-8") if new_content != old_content: file_path.write_text(new_content, encoding="utf-8") - print( + logger.info( f"Updated registry module '{source}' in {file_path.relative_to(ROOT)} to {new_version}" ) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 747e675..5b24a31 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -57,10 +57,10 @@ def test_managed_source_keys(): assert "git::https://github.com/org/repo.git?ref=" in result assert "registry.terraform.io/org/module/aws" in result -def test_warn_on_unmanaged_modules(monkeypatch: MonkeyPatch, capsys: pytest.CaptureFixture[str]): +def test_warn_on_unmanaged_modules(monkeypatch: MonkeyPatch, caplog: pytest.LogCaptureFixture): monkeypatch.setattr(discovery, "discover_module_sources", lambda: {"source1", "source2"}) monkeypatch.setattr(discovery, "managed_source_keys", lambda _: {"source1"}) - warn_on_unmanaged_modules([]) - captured = capsys.readouterr() - assert "[WARN] Terraform modules were found in the repo but are not represented in the manifest:" in captured.out - assert " - source2" in captured.out \ No newline at end of file + with caplog.at_level("WARNING"): + warn_on_unmanaged_modules([]) + assert "Terraform modules were found in the repo but are not represented in the manifest:" in caplog.text + assert " - source2" in caplog.text \ No newline at end of file