Skip to content

Commit

Permalink
Release 8.1.0 changes: New warning level in logging + ConnectionRefus… (
Browse files Browse the repository at this point in the history
#2330)

* Release 8.1.0 changes: New warning level in logging + ConnectionRefusedError logic

* Fixes typo

* Ruff

---------

Co-authored-by: Roman <167799377+roman-opentensor@users.noreply.github.com>
  • Loading branch information
ibraheem-opentensor and roman-opentensor authored Oct 3, 2024
1 parent d9d0bcf commit fe3a72e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 30 deletions.
21 changes: 0 additions & 21 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@
from .utils.deprecated import *


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def __getattr__(name):
if name == "version_split":
warnings.warn(
Expand Down
13 changes: 8 additions & 5 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,18 @@ def __init__(
logging.info(
f"You are connecting to {self.network} network with endpoint {self.chain_endpoint}."
)
logging.warning(
logging.debug(
"We strongly encourage running a local subtensor node whenever possible. "
"This increases decentralization and resilience of the network."
)
logging.warning(
logging.debug(
"In a future release, local subtensor will become the default endpoint. "
"To get ahead of this change, please run a local subtensor node and point to it."
)

self.log_verbose = log_verbose
self._connection_timeout = connection_timeout
self.substrate: "SubstrateInterface" = None
self._get_substrate()

def __str__(self) -> str:
Expand All @@ -201,7 +202,8 @@ def __repr__(self) -> str:

def close(self):
"""Cleans up resources for this subtensor instance like active websocket connection and active extensions."""
self.substrate.close()
if self.substrate:
self.substrate.close()

def _get_substrate(self):
"""Establishes a connection to the Substrate node using configured parameters."""
Expand All @@ -223,14 +225,15 @@ def _get_substrate(self):
except (AttributeError, TypeError, socket.error, OSError) as e:
logging.warning(f"Error setting timeout: {e}")

except ConnectionRefusedError:
except ConnectionRefusedError as error:
logging.error(
f"Could not connect to {self.network} network with {self.chain_endpoint} chain endpoint.",
)
logging.info(
"You can check if you have connectivity by running this command: nc -vz localhost "
f"{self.chain_endpoint.split(':')[2]}"
f"{self.chain_endpoint}"
)
raise ConnectionRefusedError(error.args)

@staticmethod
def config() -> "Config":
Expand Down
69 changes: 65 additions & 4 deletions bittensor/utils/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,48 @@ class LoggingMachine(StateMachine, Logger):
Debug = State()
Trace = State()
Disabled = State()
Warning = State()

enable_default = (
Debug.to(Default)
| Trace.to(Default)
| Disabled.to(Default)
| Default.to(Default)
| Warning.to(Default)
)

enable_info = enable_default

enable_trace = (
Default.to(Trace) | Debug.to(Trace) | Disabled.to(Trace) | Trace.to(Trace)
Default.to(Trace)
| Debug.to(Trace)
| Disabled.to(Trace)
| Trace.to(Trace)
| Warning.to(Trace)
)

enable_debug = (
Default.to(Debug) | Trace.to(Debug) | Disabled.to(Debug) | Debug.to(Debug)
Default.to(Debug)
| Trace.to(Debug)
| Disabled.to(Debug)
| Debug.to(Debug)
| Warning.to(Debug)
)

enable_warning = (
Default.to(Warning)
| Trace.to(Warning)
| Disabled.to(Warning)
| Debug.to(Warning)
| Warning.to(Warning)
)

disable_trace = Trace.to(Default)

disable_debug = Debug.to(Default)

disable_warning = Warning.to(Default)

disable_logging = (
Trace.to(Disabled)
| Debug.to(Disabled)
Expand Down Expand Up @@ -301,16 +323,36 @@ def after_transition(self, event, state):
# Default Logging
def before_enable_default(self):
"""Logs status before enable Default."""
self._logger.info(f"Enabling default logging.")
self._logger.info("Enabling default logging.")
self._logger.setLevel(stdlogging.WARNING)
for logger in all_loggers():
if logger.name in self._primary_loggers:
continue
logger.setLevel(stdlogging.CRITICAL)

def before_enable_info(self):
"""Logs status before enable Default."""
self._logger.info("Enabling default logging.")
self._logger.setLevel(stdlogging.INFO)
for logger in all_loggers():
if logger.name in self._primary_loggers:
continue
logger.setLevel(stdlogging.CRITICAL)

def after_enable_default(self):
pass

def before_enable_warning(self):
"""Logs status before enable Warning."""
self._logger.info("Enabling warning.")
self._stream_formatter.set_trace(True)
for logger in all_loggers():
logger.setLevel(stdlogging.WARNING)

def after_enable_warning(self):
"""Logs status after enable Warning."""
self._logger.info("Warning enabled.")

# Trace
def before_enable_trace(self):
"""Logs status before enable Trace."""
Expand All @@ -325,7 +367,7 @@ def after_enable_trace(self):

def before_disable_trace(self):
"""Logs status before disable Trace."""
self._logger.info(f"Disabling trace.")
self._logger.info("Disabling trace.")
self._stream_formatter.set_trace(False)
self.enable_default()

Expand Down Expand Up @@ -449,6 +491,25 @@ def set_trace(self, on: bool = True):
if self.current_state_value == "Trace":
self.disable_trace()

def set_warning(self, on: bool = True):
"""Sets Warning state."""
if on and not self.current_state_value == "Warning":
self.enable_warning()
elif not on:
if self.current_state_value == "Warning":
self.disable_warning()

def set_default(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_default()

# as an option to be more obvious. `bittensor.logging.set_info()` is the same `bittensor.logging.set_default()`
def set_info(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_info()

def get_level(self) -> int:
"""Returns Logging level."""
return self._logger.level
Expand Down
33 changes: 33 additions & 0 deletions bittensor/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
)
from bittensor.utils.balance import Balance as Balance # noqa: F401
from bittensor.utils.mock.subtensor_mock import MockSubtensor as MockSubtensor # noqa: F401
from bittensor.utils.btlogging import logging
from bittensor.utils.subnets import SubnetsAPI # noqa: F401

# Backwards compatibility with previous bittensor versions.
Expand Down Expand Up @@ -148,3 +149,35 @@
# Makes the `bittensor.core.extrinsics` subpackage available as `bittensor.extrinsics` for backwards compatibility.
extrinsics_subpackage = importlib.import_module("bittensor.core.extrinsics")
sys.modules["bittensor.extrinsics"] = extrinsics_subpackage


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def warning(on: bool = True):
"""
Enables or disables warning logging.
Args:
on (bool): If True, enables warning logging. If False, disables warning logging and sets default (INFO) level.
"""
logging.set_warning(on)


# set Warning logging level for bittensor SDK
warning()

0 comments on commit fe3a72e

Please sign in to comment.