Skip to content

Commit 20b23cc

Browse files
chandrasekharan-zipstackgaya3-zipstackmuhammad-ali-e
authored
fix: Added root logger to tools (#123)
* File Storage interface and implementation (#112) * contextSizeChanges * contextSizeChanges * Version roll and test folder check in * Fix enum values * Fix test cases, address review comments * Address review comments * Update pyproject.toml Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Address mypy issues * Change class design and implementation * Remove unused definitions * Add cp() and function refactoring * Check-in sample env * Default value of dict changed to None * Add size() * Refctor for using FileStorage * Refactor to use FileStorage * Fix issues * Add mim_type, download functions * change comments * Refactor het_hash_from_file * Add return types * Remove permanent file storage from sdk * Fix SDK functional issues * Support minio * Test cases for Minio * Bring file variants back to sdk * Fix copy_on_write * Add new test cases for uploadd/download * Add new functions to support platform-service * Change modififcation_time return type to datetime * Refactor env pick-up logic * Sample env * contextSizeChanges * Remove commented code and some improvisations * contextSizeChanges * Add right JSON formatted string * Update src/unstract/sdk/file_storage/fs_permanent.py Co-authored-by: Chandrasekharan M <[email protected]> Signed-off-by: Gayathri <[email protected]> * Address review comments * Address review comments * Update src/unstract/sdk/file_storage/fs_shared_temporary.py Co-authored-by: ali <[email protected]> Signed-off-by: Gayathri <[email protected]> * Refactor for change in enum value * Add return type --------- Signed-off-by: Gayathri <[email protected]> Co-authored-by: Chandrasekharan M <[email protected]> Co-authored-by: ali <[email protected]> * Added root logger for tools * Avoid adding multiple handlers to rootLogger to prevent duplicate logs * Revert "File Storage interface and implementation (#112)" This reverts commit ef00f81. --------- Signed-off-by: Gayathri <[email protected]> Co-authored-by: Gayathri <[email protected]> Co-authored-by: ali <[email protected]>
1 parent 5f51fe4 commit 20b23cc

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/unstract/sdk/prompt.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,19 @@ def get_exported_tool(
146146
"""
147147
platform_host = tool.get_env_or_die(ToolEnv.PLATFORM_HOST)
148148
platform_port = tool.get_env_or_die(ToolEnv.PLATFORM_PORT)
149-
150-
tool.stream_log("Connecting to DB and getting exported tool metadata")
151149
base_url = SdkHelper.get_platform_base_url(platform_host, platform_port)
152150
bearer_token = tool.get_env_or_die(ToolEnv.PLATFORM_API_KEY)
153-
154151
url = f"{base_url}/custom_tool_instance"
155152
query_params = {PromptStudioKeys.PROMPT_REGISTRY_ID: prompt_registry_id}
156153
headers = {"Authorization": f"Bearer {bearer_token}"}
157154
response = requests.get(url, headers=headers, params=query_params)
158155
if response.status_code == 200:
159-
adapter_data: dict[str, Any] = response.json()
160-
tool.stream_log(
161-
"Successfully retrieved metadata for the exported "
162-
f"tool: {prompt_registry_id}"
163-
)
164-
return adapter_data
165-
156+
return response.json()
166157
elif response.status_code == 404:
167158
tool.stream_error_and_exit(
168-
f"Exported tool {prompt_registry_id} is not found"
159+
f"Exported tool '{prompt_registry_id}' is not found"
169160
)
170161
return None
171-
172162
else:
173163
tool.stream_error_and_exit(
174164
f"Error while retrieving tool metadata "

src/unstract/sdk/tool/stream.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import datetime
22
import json
3+
import logging
34
import os
45
from typing import Any
56

67
from deprecated import deprecated
78

89
from unstract.sdk.constants import Command, LogLevel, LogStage, ToolEnv
910
from unstract.sdk.utils import ToolUtils
11+
from unstract.sdk.utils.common_utils import UNSTRACT_TO_PY_LOG_LEVEL
1012

1113

1214
class StreamMixin:
@@ -30,8 +32,35 @@ def __init__(self, log_level: LogLevel = LogLevel.INFO, **kwargs) -> None:
3032
self._exec_by_tool = ToolUtils.str_to_bool(
3133
os.environ.get(ToolEnv.EXECUTION_BY_TOOL, "False")
3234
)
35+
if self.is_exec_by_tool:
36+
self._configure_logger()
3337
super().__init__(**kwargs)
3438

39+
@property
40+
def is_exec_by_tool(self):
41+
"""Flag to determine if SDK library is used in a tool's context.
42+
43+
Returns:
44+
bool: True if SDK is used by a tool else False
45+
"""
46+
return self._exec_by_tool
47+
48+
def _configure_logger(self) -> None:
49+
"""Helps configure the logger for the tool run."""
50+
rootlogger = logging.getLogger("")
51+
# Avoids adding multiple handlers
52+
if rootlogger.hasHandlers():
53+
return
54+
handler = logging.StreamHandler()
55+
handler.setLevel(level=UNSTRACT_TO_PY_LOG_LEVEL[self.log_level])
56+
handler.setFormatter(
57+
logging.Formatter(
58+
"[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
59+
)
60+
)
61+
rootlogger.addHandler(handler)
62+
rootlogger.setLevel(level=UNSTRACT_TO_PY_LOG_LEVEL[self.log_level])
63+
3564
def stream_log(
3665
self,
3766
log: str,

src/unstract/sdk/utils/common_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ def generate_uuid() -> str:
2323
logging.ERROR: LogLevel.ERROR,
2424
}
2525

26+
# Mapping from Unstract log level to python counterpart
27+
UNSTRACT_TO_PY_LOG_LEVEL = {
28+
LogLevel.DEBUG: logging.DEBUG,
29+
LogLevel.INFO: logging.INFO,
30+
LogLevel.WARN: logging.WARNING,
31+
LogLevel.ERROR: logging.ERROR,
32+
}
33+
2634

2735
def log_elapsed(operation):
2836
"""Adds an elapsed time log.

0 commit comments

Comments
 (0)