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

Improved logic for concatenating message, prefix, and suffix in bittensor logging + test #2306

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions bittensor/utils/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
from .helpers import all_loggers


def _concat_message(msg="", prefix="", suffix=""):
"""Concatenates a message with optional prefix and suffix."""
msg = f"{f'{prefix} - ' if prefix else ''}{msg}{f' - {suffix}' if suffix else ''}"
return msg


class LoggingConfig(NamedTuple):
"""Named tuple to hold the logging configuration."""

Expand Down Expand Up @@ -363,17 +369,17 @@ def __trace_on__(self) -> bool:

def trace(self, msg="", prefix="", suffix="", *args, **kwargs):
"""Wraps trace message with prefix and suffix."""
msg = f"{prefix} - {msg} - {suffix}"
msg = _concat_message(msg, prefix, suffix)
self._logger.trace(msg, *args, **kwargs)

def debug(self, msg="", prefix="", suffix="", *args, **kwargs):
"""Wraps debug message with prefix and suffix."""
msg = f"{prefix} - {msg} - {suffix}"
msg = _concat_message(msg, prefix, suffix)
self._logger.debug(msg, *args, **kwargs)

def info(self, msg="", prefix="", suffix="", *args, **kwargs):
"""Wraps info message with prefix and suffix."""
msg = f"{prefix} - {msg} - {suffix}"
msg = _concat_message(msg, prefix, suffix)
self._logger.info(msg, *args, **kwargs)

def success(self, msg="", prefix="", suffix="", *args, **kwargs):
Expand Down
24 changes: 23 additions & 1 deletion tests/unit_tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
DEFAULT_LOG_FILE_NAME,
BITTENSOR_LOGGER_NAME,
)
from bittensor.utils.btlogging.loggingmachine import LoggingConfig
from bittensor.utils.btlogging.loggingmachine import LoggingConfig, _concat_message


@pytest.fixture(autouse=True, scope="session")
Expand Down Expand Up @@ -175,3 +175,25 @@ def test_all_log_levels_output(logging_machine, caplog):
assert "Test warning" in caplog.text
assert "Test error" in caplog.text
assert "Test critical" in caplog.text


@pytest.mark.parametrize(
"msg, prefix, suffix, expected_result",
[
("msg", "", "", "msg"),
("msg", None, None, "msg"),
("msg", "prefix", None, "prefix - msg"),
("msg", None, "suffix", "msg - suffix"),
("msg", "prefix", "suffix", "prefix - msg - suffix"),
],
ids=[
"message, no prefix (str), no suffix (str)",
"message, no prefix (None), no suffix (None)",
"message and prefix only",
"message and suffix only",
"message, prefix, and suffix",
],
)
def test_concat(msg, prefix, suffix, expected_result):
"""Test different options of message concatenation with prefix and suffix."""
assert _concat_message(msg, prefix, suffix) == expected_result