Skip to content

Commit 3f5ff67

Browse files
committed
refactor(logging): replace warnings with logger across codebase
1 parent 62c7191 commit 3f5ff67

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

src/gitingest/entrypoint.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import shutil
88
import stat
99
import sys
10-
import warnings
1110
from contextlib import asynccontextmanager
1211
from pathlib import Path
1312
from typing import TYPE_CHECKING, AsyncGenerator, Callable
@@ -241,19 +240,19 @@ def _override_branch_and_tag(query: IngestionQuery, branch: str | None, tag: str
241240
"""
242241
if tag and query.tag and tag != query.tag:
243242
msg = f"Warning: The specified tag '{tag}' overrides the tag found in the URL '{query.tag}'."
244-
warnings.warn(msg, RuntimeWarning, stacklevel=3)
243+
logger.warning(msg)
245244

246245
query.tag = tag or query.tag
247246

248247
if branch and query.branch and branch != query.branch:
249248
msg = f"Warning: The specified branch '{branch}' overrides the branch found in the URL '{query.branch}'."
250-
warnings.warn(msg, RuntimeWarning, stacklevel=3)
249+
logger.warning(msg)
251250

252251
query.branch = branch or query.branch
253252

254253
if tag and branch:
255254
msg = "Warning: Both tag and branch are specified. The tag will be used."
256-
warnings.warn(msg, RuntimeWarning, stacklevel=3)
255+
logger.warning(msg)
257256

258257
# Tag wins over branch if both supplied
259258
if query.tag:

src/gitingest/output_formatter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import ssl
6-
import warnings
76
from typing import TYPE_CHECKING
87

98
import requests.exceptions
@@ -197,11 +196,11 @@ def _format_token_count(text: str) -> str | None:
197196
encoding = tiktoken.get_encoding("o200k_base") # gpt-4o, gpt-4o-mini
198197
total_tokens = len(encoding.encode(text, disallowed_special=()))
199198
except (ValueError, UnicodeEncodeError) as exc:
200-
warnings.warn(f"Failed to estimate token size: {exc}", RuntimeWarning, stacklevel=3)
199+
logger.warning("Failed to estimate token size", extra={"error": str(exc)})
201200
return None
202201
except (requests.exceptions.RequestException, ssl.SSLError) as exc:
203202
# If network errors, skip token count estimation instead of erroring out
204-
warnings.warn(f"Failed to download tiktoken model: {exc}", RuntimeWarning, stacklevel=3)
203+
logger.warning("Failed to download tiktoken model", extra={"error": str(exc)})
205204
return None
206205

207206
for threshold, suffix in _TOKEN_THRESHOLDS:

src/gitingest/query_parser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from __future__ import annotations
44

55
import uuid
6-
import warnings
76
from pathlib import Path
87
from typing import Literal
98

109
from gitingest.config import TMP_BASE_PATH
1110
from gitingest.schemas import IngestionQuery
1211
from gitingest.utils.git_utils import fetch_remote_branches_or_tags, resolve_commit
12+
from gitingest.utils.logging_config import get_logger
1313
from gitingest.utils.query_parser_utils import (
1414
PathKind,
1515
_fallback_to_root,
@@ -18,6 +18,9 @@
1818
_normalise_source,
1919
)
2020

21+
# Initialize logger for this module
22+
logger = get_logger(__name__)
23+
2124

2225
async def parse_remote_repo(source: str, token: str | None = None) -> IngestionQuery:
2326
"""Parse a repository URL and return an ``IngestionQuery`` object.
@@ -169,7 +172,7 @@ async def _configure_branch_or_tag(
169172
except RuntimeError as exc:
170173
# If remote discovery fails, we optimistically treat the first path segment as the branch/tag.
171174
msg = f"Warning: Failed to fetch {_ref_type}: {exc}"
172-
warnings.warn(msg, RuntimeWarning, stacklevel=2)
175+
logger.warning(msg)
173176
return path_parts.pop(0) if path_parts else None
174177

175178
# Iterate over the path components and try to find a matching branch/tag

src/gitingest/utils/notebook.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
from __future__ import annotations
44

55
import json
6-
import warnings
76
from itertools import chain
87
from typing import TYPE_CHECKING, Any
98

109
from gitingest.utils.exceptions import InvalidNotebookError
10+
from gitingest.utils.logging_config import get_logger
1111

1212
if TYPE_CHECKING:
1313
from pathlib import Path
1414

15+
# Initialize logger for this module
16+
logger = get_logger(__name__)
17+
1518

1619
def process_notebook(file: Path, *, include_output: bool = True) -> str:
1720
"""Process a Jupyter notebook file and return an executable Python script as a string.
@@ -44,20 +47,16 @@ def process_notebook(file: Path, *, include_output: bool = True) -> str:
4447
# Check if the notebook contains worksheets
4548
worksheets = notebook.get("worksheets")
4649
if worksheets:
47-
warnings.warn(
50+
logger.warning(
4851
"Worksheets are deprecated as of IPEP-17. Consider updating the notebook. "
4952
"(See: https://github.com/jupyter/nbformat and "
5053
"https://github.com/ipython/ipython/wiki/IPEP-17:-Notebook-Format-4#remove-multiple-worksheets "
5154
"for more information.)",
52-
DeprecationWarning,
53-
stacklevel=2,
5455
)
5556

5657
if len(worksheets) > 1:
57-
warnings.warn(
58+
logger.warning(
5859
"Multiple worksheets detected. Combining all worksheets into a single script.",
59-
UserWarning,
60-
stacklevel=2,
6160
)
6261

6362
cells = list(chain.from_iterable(ws["cells"] for ws in worksheets))

src/gitingest/utils/query_parser_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
from __future__ import annotations
44

55
import string
6-
import warnings
76
from typing import TYPE_CHECKING, cast
87
from urllib.parse import ParseResult, unquote, urlparse
98

109
from gitingest.utils.compat_typing import StrEnum
1110
from gitingest.utils.git_utils import _resolve_ref_to_sha, check_repo_exists
11+
from gitingest.utils.logging_config import get_logger
1212

1313
if TYPE_CHECKING:
1414
from gitingest.schemas import IngestionQuery
1515

16+
# Initialize logger for this module
17+
logger = get_logger(__name__)
1618

1719
HEX_DIGITS: set[str] = set(string.hexdigits)
1820

@@ -56,7 +58,7 @@ async def _fallback_to_root(query: IngestionQuery, token: str | None, warn_msg:
5658
url = cast("str", query.url)
5759
query.commit = await _resolve_ref_to_sha(url, pattern="HEAD", token=token)
5860
if warn_msg:
59-
warnings.warn(warn_msg, RuntimeWarning, stacklevel=3)
61+
logger.warning(warn_msg)
6062
return query
6163

6264

0 commit comments

Comments
 (0)