logger.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from .handler import handler
  2. class logger:
  3. def __init__(self) -> None:
  4. self.__handlers = set()
  5. def _get_message(self, *args, **kwargs) -> str:
  6. if len(kwargs) == 0:
  7. return self.__get_message_from_args(self, *args)
  8. if len(args) != 1:
  9. raise RuntimeError("You must specify output format.")
  10. return self.__get_message_from_kwargs(self, args[0], **kwargs)
  11. def __get_message_from_args(self, *args) -> str:
  12. line = ""
  13. for count in args:
  14. typed = type(count)
  15. if typed is str:
  16. line = line + count + " "
  17. continue
  18. if typed is int or typed is float or typed is bool:
  19. line = line + str(count) + " "
  20. continue
  21. line = line + repr(count) + " "
  22. if len(args) > 0:
  23. line = line[0:-1]
  24. return line
  25. def __get_message_from_kwargs(self, formation: str, **kwargs) -> str:
  26. return formation.format(**kwargs)
  27. def use_handler(self, target: handler) -> None:
  28. self.__handlers.add(target)