Skip to content

Commit 7208bff

Browse files
authored
Merge pull request #1053 from Vikrant-Khedkar/fix/replace-print-with-logging
fix: replace print() statements with proper logging across codebase
2 parents cf9b87e + 1d9551a commit 7208bff

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
lines changed

scrapegraphai/graphs/abstract_graph.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ def _create_llm(self, llm_config: dict) -> object:
190190
If possible, try to use a model instance instead."""
191191
)
192192
llm_params["model_provider"] = possible_providers[0]
193-
print(
194-
(
195-
f"Found providers {possible_providers} for model {llm_params['model']}, using {llm_params['model_provider']}.\n"
196-
"If it was not intended please specify the model provider in the graph configuration"
197-
)
193+
logger.info(
194+
"Found providers %s for model %s, using %s. "
195+
"If it was not intended please specify the model provider in the graph configuration",
196+
possible_providers,
197+
llm_params["model"],
198+
llm_params["model_provider"],
198199
)
199200

200201
if llm_params["model_provider"] not in known_providers:
@@ -209,10 +210,12 @@ def _create_llm(self, llm_config: dict) -> object:
209210
llm_params["model"]
210211
]
211212
except KeyError:
212-
print(
213-
f"""Max input tokens for model {llm_params["model_provider"]}/{llm_params["model"]} not found,
214-
please specify the model_tokens parameter in the llm section of the graph configuration.
215-
Using default token size: 8192"""
213+
logger.warning(
214+
"Max input tokens for model %s/%s not found, "
215+
"please specify the model_tokens parameter in the llm section of the graph configuration. "
216+
"Using default token size: 8192",
217+
llm_params["model_provider"],
218+
llm_params["model"],
216219
)
217220
self.model_token = 8192
218221
else:

scrapegraphai/graphs/base_graph.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,16 @@ def execute(self, initial_state: dict) -> Tuple[dict, list]:
362362
else:
363363
state, exec_info = self._execute_standard(initial_state)
364364

365-
# Print the result first
366365
if "answer" in state:
367-
print(state["answer"])
366+
logger.info(state["answer"])
368367
elif "parsed_doc" in state:
369-
print(state["parsed_doc"])
368+
logger.info(state["parsed_doc"])
370369
elif "generated_code" in state:
371-
print(state["generated_code"])
370+
logger.info(state["generated_code"])
372371
elif "merged_script" in state:
373-
print(state["merged_script"])
372+
logger.info(state["merged_script"])
374373

375-
# Then show the message ONLY ONCE
376-
print(f"✨ Try enhanced version of ScrapegraphAI at {CLICKABLE_URL} ✨")
374+
logger.info("✨ Try enhanced version of ScrapegraphAI at %s ✨", CLICKABLE_URL)
377375

378376
return state, exec_info
379377

scrapegraphai/graphs/speech_graph.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
from ..models import OpenAITextToSpeech
1010
from ..nodes import FetchNode, GenerateAnswerNode, ParseNode, TextToSpeechNode
11+
from ..utils.logging import get_logger
1112
from ..utils.save_audio_from_bytes import save_audio_from_bytes
1213
from .abstract_graph import AbstractGraph
1314
from .base_graph import BaseGraph
1415

16+
logger = get_logger(__name__)
17+
1518

1619
class SpeechGraph(AbstractGraph):
1720
"""
@@ -112,6 +115,6 @@ def run(self) -> str:
112115
if not audio:
113116
raise ValueError("No audio generated from the text.")
114117
save_audio_from_bytes(audio, self.config.get("output_path", "output.mp3"))
115-
print(f"Audio saved to {self.config.get('output_path', 'output.mp3')}")
118+
logger.info("Audio saved to %s", self.config.get("output_path", "output.mp3"))
116119

117120
return self.final_state.get("answer", "No answer found.")

scrapegraphai/integrations/burr_bridge.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import uuid
99
from typing import Any, Dict, List, Tuple
1010

11+
from ..utils.logging import get_logger
12+
13+
logger = get_logger(__name__)
14+
1115
try:
1216
from burr import tracking
1317
from burr.core import (
@@ -32,10 +36,10 @@ class PrintLnHook(PostRunStepHook, PreRunStepHook):
3236
"""
3337

3438
def pre_run_step(self, *, state: "State", action: "Action", **future_kwargs: Any):
35-
print(f"Starting action: {action.name}")
39+
logger.debug("Starting action: %s", action.name)
3640

3741
def post_run_step(self, *, state: "State", action: "Action", **future_kwargs: Any):
38-
print(f"Finishing action: {action.name}")
42+
logger.debug("Finishing action: %s", action.name)
3943

4044

4145
class BurrNodeBridge(Action):

scrapegraphai/utils/data_export.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import xml.etree.ElementTree as ET
99
from typing import Any, Dict, List
1010

11+
from .logging import get_logger
12+
13+
logger = get_logger(__name__)
14+
1115

1216
def export_to_json(data: List[Dict[str, Any]], filename: str) -> None:
1317
"""
@@ -18,7 +22,7 @@ def export_to_json(data: List[Dict[str, Any]], filename: str) -> None:
1822
"""
1923
with open(filename, "w", encoding="utf-8") as f:
2024
json.dump(data, f, ensure_ascii=False, indent=4)
21-
print(f"Data exported to {filename}")
25+
logger.info("Data exported to %s", filename)
2226

2327

2428
def export_to_csv(data: List[Dict[str, Any]], filename: str) -> None:
@@ -29,15 +33,15 @@ def export_to_csv(data: List[Dict[str, Any]], filename: str) -> None:
2933
:param filename: Name of the file to save the CSV data
3034
"""
3135
if not data:
32-
print("No data to export")
36+
logger.warning("No data to export")
3337
return
3438

3539
keys = data[0].keys()
3640
with open(filename, "w", newline="", encoding="utf-8") as f:
3741
writer = csv.DictWriter(f, fieldnames=keys)
3842
writer.writeheader()
3943
writer.writerows(data)
40-
print(f"Data exported to {filename}")
44+
logger.info("Data exported to %s", filename)
4145

4246

4347
def export_to_xml(
@@ -59,4 +63,4 @@ def export_to_xml(
5963

6064
tree = ET.ElementTree(root)
6165
tree.write(filename, encoding="utf-8", xml_declaration=True)
62-
print(f"Data exported to {filename}")
66+
logger.info("Data exported to %s", filename)

scrapegraphai/utils/screenshot_scraping/screenshot_preparation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import numpy as np
88
from playwright.async_api import async_playwright
99

10+
from ..logging import get_logger
11+
12+
logger = get_logger(__name__)
13+
1014

1115
async def take_screenshot(url: str, save_path: str = None, quality: int = 100):
1216
"""
@@ -155,7 +159,7 @@ def select_area_with_ipywidget(image):
155159

156160
img_array = np.array(image)
157161

158-
print(img_array.shape)
162+
logger.debug("Image array shape: %s", img_array.shape)
159163

160164
def update_plot(top_bottom, left_right, image_size):
161165
plt.figure(figsize=(image_size, image_size))

0 commit comments

Comments
 (0)