Skip to content

Commit 877c01b

Browse files
authored
Merge pull request #36 from opentensor/feat/roman/logging
Improve logging
2 parents da63ac5 + e93c136 commit 877c01b

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

async_substrate_interface/async_substrate.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

6262
ResultHandler = Callable[[dict, Any], Awaitable[tuple[dict, bool]]]
6363

64+
logger = logging.getLogger("async_substrate_interface")
65+
6466

6567
class AsyncExtrinsicReceipt:
6668
"""
@@ -1017,7 +1019,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10171019
if not self._metadata:
10181020
if self.runtime_version in self._metadata_cache:
10191021
# Get metadata from cache
1020-
logging.debug(
1022+
logger.debug(
10211023
"Retrieved metadata for {} from memory".format(
10221024
self.runtime_version
10231025
)
@@ -1031,7 +1033,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10311033
metadata = self._metadata = await self.get_block_metadata(
10321034
block_hash=runtime_block_hash, decode=True
10331035
)
1034-
logging.debug(
1036+
logger.debug(
10351037
"Retrieved metadata for {} from Substrate node".format(
10361038
self.runtime_version
10371039
)
@@ -1044,7 +1046,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10441046

10451047
if self.runtime_version in self._metadata_v15_cache:
10461048
# Get metadata v15 from cache
1047-
logging.debug(
1049+
logger.debug(
10481050
"Retrieved metadata v15 for {} from memory".format(
10491051
self.runtime_version
10501052
)
@@ -1056,7 +1058,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10561058
metadata_v15 = (
10571059
self._old_metadata_v15
10581060
) = await self._load_registry_at_block(block_hash=runtime_block_hash)
1059-
logging.debug(
1061+
logger.debug(
10601062
"Retrieved metadata v15 for {} from Substrate node".format(
10611063
self.runtime_version
10621064
)
@@ -1069,7 +1071,7 @@ async def get_runtime(block_hash, block_id) -> Runtime:
10691071
self.reload_type_registry(use_remote_preset=False, auto_discover=True)
10701072

10711073
if self.implements_scaleinfo:
1072-
logging.debug("Add PortableRegistry from metadata to type registry")
1074+
logger.debug("Add PortableRegistry from metadata to type registry")
10731075
self.runtime_config.add_portable_registry(metadata)
10741076

10751077
# Set active runtime version
@@ -1988,14 +1990,14 @@ async def _make_rpc_request(
19881990
break
19891991
if time.time() - self.ws.last_received >= self.retry_timeout:
19901992
if attempt >= self.max_retries:
1991-
logging.warning(
1993+
logger.warning(
19921994
f"Timed out waiting for RPC requests {attempt} times. Exiting."
19931995
)
19941996
raise SubstrateRequestException("Max retries reached.")
19951997
else:
19961998
self.ws.last_received = time.time()
19971999
await self.ws.connect(force=True)
1998-
logging.error(
2000+
logger.error(
19992001
f"Timed out waiting for RPC requests. "
20002002
f"Retrying attempt {attempt + 1} of {self.max_retries}"
20012003
)
@@ -2067,7 +2069,7 @@ async def rpc_request(
20672069
"Failed to get runtime version"
20682070
in result[payload_id][0]["error"]["message"]
20692071
):
2070-
logging.warning(
2072+
logger.warning(
20712073
"Failed to get runtime. Re-fetching from chain, and retrying."
20722074
)
20732075
await self.init_runtime()
@@ -2544,7 +2546,7 @@ async def _do_runtime_call_old(
25442546
params: Optional[Union[list, dict]] = None,
25452547
block_hash: Optional[str] = None,
25462548
) -> ScaleType:
2547-
logging.debug(
2549+
logger.debug(
25482550
f"Decoding old runtime call: {api}.{method} with params: {params} at block hash: {block_hash}"
25492551
)
25502552
runtime_call_def = _TYPE_REGISTRY["runtime_api"][api]["methods"][method]
@@ -2582,7 +2584,7 @@ async def _do_runtime_call_old(
25822584
# Get correct type
25832585
result_decoded = runtime_call_def["decoder"](bytes(result_bytes))
25842586
as_dict = _bt_decode_to_dict_or_list(result_decoded)
2585-
logging.debug("Decoded old runtime call result: ", as_dict)
2587+
logger.debug("Decoded old runtime call result: ", as_dict)
25862588
result_obj = ScaleObj(as_dict)
25872589

25882590
return result_obj

async_substrate_interface/sync_substrate.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
ResultHandler = Callable[[dict, Any], tuple[dict, bool]]
4343

44+
logger = logging.getLogger("async_substrate_interface")
45+
4446

4547
class ExtrinsicReceipt:
4648
"""
@@ -768,7 +770,7 @@ def get_runtime(block_hash, block_id) -> Runtime:
768770
if not self._metadata:
769771
if self.runtime_version in self._metadata_cache:
770772
# Get metadata from cache
771-
logging.debug(
773+
logger.debug(
772774
"Retrieved metadata for {} from memory".format(
773775
self.runtime_version
774776
)
@@ -780,7 +782,7 @@ def get_runtime(block_hash, block_id) -> Runtime:
780782
metadata = self._metadata = self.get_block_metadata(
781783
block_hash=runtime_block_hash, decode=True
782784
)
783-
logging.debug(
785+
logger.debug(
784786
"Retrieved metadata for {} from Substrate node".format(
785787
self.runtime_version
786788
)
@@ -793,7 +795,7 @@ def get_runtime(block_hash, block_id) -> Runtime:
793795

794796
if self.runtime_version in self._metadata_v15_cache:
795797
# Get metadata v15 from cache
796-
logging.debug(
798+
logger.debug(
797799
"Retrieved metadata v15 for {} from memory".format(
798800
self.runtime_version
799801
)
@@ -805,7 +807,7 @@ def get_runtime(block_hash, block_id) -> Runtime:
805807
metadata_v15 = self._old_metadata_v15 = self._load_registry_at_block(
806808
block_hash=runtime_block_hash
807809
)
808-
logging.debug(
810+
logger.debug(
809811
"Retrieved metadata v15 for {} from Substrate node".format(
810812
self.runtime_version
811813
)
@@ -817,7 +819,7 @@ def get_runtime(block_hash, block_id) -> Runtime:
817819
self.reload_type_registry(use_remote_preset=False, auto_discover=True)
818820

819821
if self.implements_scaleinfo:
820-
logging.debug("Add PortableRegistry from metadata to type registry")
822+
logger.debug("Add PortableRegistry from metadata to type registry")
821823
self.runtime_config.add_portable_registry(metadata)
822824

823825
# Set active runtime version
@@ -1690,7 +1692,7 @@ def _make_rpc_request(
16901692
response = json.loads(ws.recv(timeout=self.retry_timeout, decode=False))
16911693
except (TimeoutError, ConnectionClosed):
16921694
if attempt >= self.max_retries:
1693-
logging.warning(
1695+
logger.warning(
16941696
f"Timed out waiting for RPC requests {attempt} times. Exiting."
16951697
)
16961698
raise SubstrateRequestException("Max retries reached.")
@@ -1801,7 +1803,7 @@ def rpc_request(
18011803
"Failed to get runtime version"
18021804
in result[payload_id][0]["error"]["message"]
18031805
):
1804-
logging.warning(
1806+
logger.warning(
18051807
"Failed to get runtime. Re-fetching from chain, and retrying."
18061808
)
18071809
self.init_runtime()
@@ -2265,7 +2267,7 @@ def _do_runtime_call_old(
22652267
params: Optional[Union[list, dict]] = None,
22662268
block_hash: Optional[str] = None,
22672269
) -> ScaleType:
2268-
logging.debug(
2270+
logger.debug(
22692271
f"Decoding old runtime call: {api}.{method} with params: {params} at block hash: {block_hash}"
22702272
)
22712273
runtime_call_def = _TYPE_REGISTRY["runtime_api"][api]["methods"][method]
@@ -2301,7 +2303,7 @@ def _do_runtime_call_old(
23012303
# Get correct type
23022304
result_decoded = runtime_call_def["decoder"](bytes(result_bytes))
23032305
as_dict = _bt_decode_to_dict_or_list(result_decoded)
2304-
logging.debug("Decoded old runtime call result: ", as_dict)
2306+
logger.debug("Decoded old runtime call result: ", as_dict)
23052307
result_obj = ScaleObj(as_dict)
23062308

23072309
return result_obj

async_substrate_interface/types.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
from scalecodec.types import GenericCall, ScaleType
1515

1616

17+
logger = logging.getLogger("async_substrate_interface")
18+
19+
1720
class RuntimeCache:
1821
blocks: dict[int, "Runtime"]
1922
block_hashes: dict[str, "Runtime"]
@@ -644,7 +647,7 @@ def apply_type_registry_presets(
644647
type_registry_preset_dict = load_type_registry_preset(
645648
type_registry_name
646649
)
647-
logging.debug(
650+
logger.debug(
648651
f"Auto set type_registry_preset to {type_registry_name} ..."
649652
)
650653
self.type_registry_preset = type_registry_name

0 commit comments

Comments
 (0)