Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements and fixes #2510

Merged
4 changes: 2 additions & 2 deletions bittensor/core/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def __init__(
self.config = config # type: ignore

# Get wallet or use default.
self.wallet = wallet or Wallet()
self.wallet = wallet or Wallet(config=self.config)

# Build axon objects.
self.uuid = str(uuid.uuid1())
Expand Down Expand Up @@ -594,7 +594,7 @@ def config(cls) -> "Config":
"""
parser = argparse.ArgumentParser()
Axon.add_args(parser) # Add specific axon-related arguments
return Config(parser, args=[])
return Config(parser)

@classmethod
def help(cls):
Expand Down
5 changes: 4 additions & 1 deletion bittensor/core/dendrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ def log_exception(self, exception: Exception):
"""
error_id = str(uuid.uuid4())
error_type = exception.__class__.__name__
logging.error(f"{error_type}#{error_id}: {exception}")
if isinstance(exception, (aiohttp.ClientConnectorError, asyncio.TimeoutError)):
logging.debug(f"{error_type}#{error_id}: {exception}")
else:
logging.error(f"{error_type}#{error_id}: {exception}")

def process_error_message(
self,
Expand Down
1 change: 1 addition & 0 deletions bittensor/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
"logging": {
"debug": os.getenv("BT_LOGGING_DEBUG") or False,
"trace": os.getenv("BT_LOGGING_TRACE") or False,
"info": os.getenv("BT_LOGGING_INFO") or False,
"record_log": os.getenv("BT_LOGGING_RECORD_LOG") or False,
"logging_dir": os.getenv("BT_LOGGING_LOGGING_DIR") or str(MINERS_DIR),
},
Expand Down
2 changes: 1 addition & 1 deletion bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def config() -> "Config":
"""
parser = argparse.ArgumentParser()
Subtensor.add_args(parser)
return Config(parser, args=[])
return Config(parser)

@staticmethod
def setup_config(network: Optional[str], config: "Config"):
Expand Down
2 changes: 1 addition & 1 deletion bittensor/core/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def config(cls) -> "Config":
"""
parser = argparse.ArgumentParser()
PriorityThreadPoolExecutor.add_args(parser)
return Config(parser, args=[])
return Config(parser)

@property
def is_empty(self):
Expand Down
83 changes: 63 additions & 20 deletions bittensor/utils/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class LoggingConfig(NamedTuple):

debug: bool
trace: bool
info: bool
record_log: bool
logging_dir: str

Expand All @@ -78,13 +79,15 @@ class LoggingMachine(StateMachine, Logger):
Trace = State()
Disabled = State()
Warning = State()
Info = State()

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

enable_console = (
Expand All @@ -93,16 +96,25 @@ class LoggingMachine(StateMachine, Logger):
| Disabled.to(Debug)
| Debug.to(Debug)
| Warning.to(Debug)
| Info.to(Debug)
)

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

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

enable_debug = (
Expand All @@ -111,6 +123,7 @@ class LoggingMachine(StateMachine, Logger):
| Disabled.to(Debug)
| Debug.to(Debug)
| Warning.to(Debug)
| Info.to(Debug)
)

enable_warning = (
Expand All @@ -119,6 +132,7 @@ class LoggingMachine(StateMachine, Logger):
| Disabled.to(Warning)
| Debug.to(Warning)
| Warning.to(Warning)
| Info.to(Warning)
)

disable_trace = Trace.to(Default)
Expand All @@ -127,11 +141,14 @@ class LoggingMachine(StateMachine, Logger):

disable_warning = Warning.to(Default)

disable_info = Info.to(Default)

disable_logging = (
Trace.to(Disabled)
| Debug.to(Disabled)
| Default.to(Disabled)
| Disabled.to(Disabled)
| Info.to(Disabled)
)

def __init__(self, config: "Config", name: str = BITTENSOR_LOGGER_NAME):
Expand Down Expand Up @@ -169,6 +186,8 @@ def _enable_initial_state(self, config):
self.enable_trace()
elif config.debug:
self.enable_debug()
elif config.info:
self.enable_info()
else:
self.enable_default()

Expand Down Expand Up @@ -222,6 +241,8 @@ def set_config(self, config: "Config"):
self.enable_trace()
elif self._config.debug:
self.enable_debug()
elif self._config.info:
self.enable_info()

def _create_and_start_listener(self, handlers):
"""
Expand Down Expand Up @@ -341,25 +362,17 @@ def after_transition(self, event, state):
# Default Logging
def before_enable_default(self):
"""Logs status before enable Default."""
self._logger.info("Enabling default logging.")
self._logger.info("Enabling default logging (Warning level)")
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

# Warning
def before_enable_warning(self):
"""Logs status before enable Warning."""
self._logger.info("Enabling warning.")
Expand All @@ -371,6 +384,20 @@ def after_enable_warning(self):
"""Logs status after enable Warning."""
self._logger.info("Warning enabled.")

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

def after_enable_info(self):
"""Logs status after enable info."""
self._logger.info("Info enabled.")

# Trace
def before_enable_trace(self):
"""Logs status before enable Trace."""
Expand Down Expand Up @@ -525,6 +552,14 @@ def set_trace(self, on: bool = True):
if self.current_state_value == "Trace":
self.disable_trace()

def set_info(self, on: bool = True):
"""Sets Info state."""
if on and not self.current_state_value == "Info":
self.enable_info()
elif not on:
if self.current_state_value == "Info":
self.disable_info()

def set_warning(self, on: bool = True):
"""Sets Warning state."""
if on and not self.current_state_value == "Warning":
Expand All @@ -543,12 +578,6 @@ def set_console(self):
if not self.current_state_value == "Console":
self.enable_console()

# 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 All @@ -565,6 +594,7 @@ def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None):
prefix_str = "" if prefix is None else prefix + "."
try:
default_logging_debug = os.getenv("BT_LOGGING_DEBUG") or False
default_logging_info = os.getenv("BT_LOGGING_INFO") or False
default_logging_trace = os.getenv("BT_LOGGING_TRACE") or False
default_logging_record_log = os.getenv("BT_LOGGING_RECORD_LOG") or False
default_logging_logging_dir = os.getenv(
Expand All @@ -582,6 +612,12 @@ def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None):
help="""Turn on bittensor trace level information""",
default=default_logging_trace,
)
parser.add_argument(
"--" + prefix_str + "logging.info",
action="store_true",
help="""Turn on bittensor info level information""",
default=default_logging_info,
)
parser.add_argument(
"--" + prefix_str + "logging.record_log",
action="store_true",
Expand All @@ -607,19 +643,22 @@ def config(cls) -> "Config":
"""
parser = argparse.ArgumentParser()
cls.add_args(parser)
return Config(parser, args=[])
return Config(parser)

def __call__(
self,
config: "Config" = None,
debug: bool = None,
trace: bool = None,
info: bool = None,
record_log: bool = None,
logging_dir: str = None,
):
if config is not None:
cfg = self._extract_logging_config(config)
if debug is not None:
if info is not None:
cfg.info = info
elif debug is not None:
cfg.debug = debug
elif trace is not None:
cfg.trace = trace
Expand All @@ -629,6 +668,10 @@ def __call__(
cfg.logging_dir = logging_dir
else:
cfg = LoggingConfig(
debug=debug, trace=trace, record_log=record_log, logging_dir=logging_dir
debug=debug,
trace=trace,
info=info,
record_log=record_log,
logging_dir=logging_dir,
)
self.set_config(cfg)
11 changes: 10 additions & 1 deletion bittensor/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ 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.
on (bool): If True, enables warning logging. If False, disables warning logging and sets default (WARNING) level.
"""
logging.set_warning(on)


def info(on: bool = True):
"""
Enables or disables info logging.
Args:
on (bool): If True, enables info logging. If False, disables info logging and sets default (WARNING) level.
"""
logging.set_info(on)
8 changes: 5 additions & 3 deletions tests/unit_tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def mock_config(tmp_path):
log_file_path = log_dir / DEFAULT_LOG_FILE_NAME

mock_config = LoggingConfig(
debug=False, trace=False, record_log=True, logging_dir=str(log_dir)
debug=False, trace=False, info=False, record_log=True, logging_dir=str(log_dir)
)

yield mock_config, log_file_path
Expand Down Expand Up @@ -140,15 +140,17 @@ def test_enable_file_logging_with_new_config(tmp_path):
log_file_path = log_dir / DEFAULT_LOG_FILE_NAME

# check no file handler is created
config = LoggingConfig(debug=False, trace=False, record_log=True, logging_dir=None)
config = LoggingConfig(
debug=False, trace=False, info=False, record_log=True, logging_dir=None
)
lm = LoggingMachine(config)
assert not any(
isinstance(handler, stdlogging.FileHandler) for handler in lm._handlers
)

# check file handler now exists
new_config = LoggingConfig(
debug=False, trace=False, record_log=True, logging_dir=str(log_dir)
debug=False, trace=False, info=False, record_log=True, logging_dir=str(log_dir)
)
lm.set_config(new_config)
assert any(isinstance(handler, stdlogging.FileHandler) for handler in lm._handlers)
Expand Down
Loading