Skip to content

Commit

Permalink
Move HandlerMode definition
Browse files Browse the repository at this point in the history
  • Loading branch information
borzunov committed Sep 6, 2021
1 parent 18ec6ef commit 69b8acb
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions hivemind/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class TextStyle:
_codes.update({_name: "" for _name in list(_codes) if _name.isupper()})


_PACKAGE_NAME = __name__.split(".")[0]


class CustomFormatter(logging.Formatter):
"""
A formatter that allows a log time and caller info to be overridden via
Expand All @@ -56,7 +59,7 @@ def format(self, record: logging.LogRecord) -> str:

if not hasattr(record, "caller"):
module_path = record.name.split(".")
if module_path[0] == "hivemind":
if module_path[0] == _PACKAGE_NAME:
module_path = module_path[1:]
record.caller = f"{'.'.join(module_path)}.{record.funcName}:{record.lineno}"

Expand All @@ -68,7 +71,11 @@ def format(self, record: logging.LogRecord) -> str:
return super().format(record)


_PACKAGE_NAME = __name__.split(".")[0]
class HandlerMode(Enum):
NOWHERE = 0
IN_HIVEMIND = 1
IN_ROOT_LOGGER = 2


_init_lock = threading.RLock()
_current_mode = HandlerMode.NOWHERE # This is the initial state before module initialization but not an actual default
Expand Down Expand Up @@ -116,12 +123,6 @@ def _disable_default_handler(name: str) -> None:
logger.setLevel(logging.NOTSET)


class HandlerMode(Enum):
NOWHERE = 0
IN_HIVEMIND = 1
IN_ROOT_LOGGER = 2


def use_hivemind_log_handler(where: Union[HandlerMode, str]) -> None:
"""
Choose loggers where the default hivemind log handler is applied. Options for the ``where`` argument are:
Expand Down

0 comments on commit 69b8acb

Please sign in to comment.