Skip to content

Commit

Permalink
Merge pull request #2294 from opentensor/fix/revert-logging-7.4
Browse files Browse the repository at this point in the history
Reverts logging enhancement 7.4.0
  • Loading branch information
ibraheem-opentensor authored Sep 9, 2024
2 parents 1c08a9d + 1785615 commit eb10bc8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 73 deletions.
64 changes: 26 additions & 38 deletions bittensor/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import os
import sys
from logging.handlers import QueueHandler, QueueListener, RotatingFileHandler
from logging import Logger
from typing import NamedTuple

from statemachine import State, StateMachine
Expand All @@ -55,7 +56,7 @@ class LoggingConfig(NamedTuple):
logging_dir: str


class LoggingMachine(StateMachine):
class LoggingMachine(StateMachine, Logger):
"""Handles logger states for bittensor and 3rd party libraries."""

Default = State(initial=True)
Expand Down Expand Up @@ -360,58 +361,45 @@ def __trace_on__(self) -> bool:
"""
return self.current_state_value == "Trace"

@staticmethod
def _concat_msg(*args):
return " - ".join(str(el) for el in args if el != "")

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

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

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

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

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

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

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

def exception(self, msg="", *args, prefix="", suffix="", **kwargs):
def exception(self, msg="", prefix="", suffix="", *args, **kwargs):
"""Wraps exception message with prefix and suffix."""
msg = self._concat_msg(prefix, msg, suffix)
stacklevel = 2
if (
sys.implementation.name == "cpython"
and sys.version_info.major == 3
and sys.version_info.minor < 11
):
# Note that, on CPython < 3.11, exception() calls through to
# error() without adjusting stacklevel, so we have to increment it.
stacklevel += 1
self._logger.exception(msg, *args, **kwargs, stacklevel=stacklevel)
msg = f"{prefix} - {msg} - {suffix}"
self._logger.exception(msg, *args, **kwargs)

def on(self):
"""Enable default state."""
Expand Down
35 changes: 0 additions & 35 deletions tests/unit_tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os
import re
import pytest
import multiprocessing
import logging as stdlogging
Expand Down Expand Up @@ -170,36 +168,3 @@ 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


def test_log_sanity(logging_machine, caplog):
"""
Test that logging is sane:
- prefix and suffix work
- format strings work
- reported filename is correct
Note that this is tested against caplog, which is not formatted the same as
stdout.
"""
basemsg = "logmsg #%d, cookie: %s"
cookie = "0ef852c74c777f8d8cc09d511323ce76"
nfixtests = [
{},
{"prefix": "pref"},
{"suffix": "suff"},
{"prefix": "pref", "suffix": "suff"},
]
cookiejar = {}
for i, nfix in enumerate(nfixtests):
prefix = nfix.get("prefix", "")
suffix = nfix.get("suffix", "")
use_cookie = f"{cookie} #{i}#"
logging_machine.info(basemsg, i, use_cookie, prefix=prefix, suffix=suffix)
# Check to see if all elements are present, regardless of downstream formatting.
expect = f"INFO.*{os.path.basename(__file__)}.* "
if prefix != "":
expect += prefix + " - "
expect += basemsg % (i, use_cookie)
if suffix != "":
expect += " - " + suffix
assert re.search(expect, caplog.text)

0 comments on commit eb10bc8

Please sign in to comment.