logger.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. class Logger(object):
  4. """
  5. Global logger
  6. """
  7. TPL = '[{cls}] {msg}'
  8. def __init__(self, logger_name):
  9. self._name = logger_name
  10. self._logger = logging.getLogger(self._name)
  11. @classmethod
  12. def _txt(cls, instance_or_class):
  13. if instance_or_class.__class__.__name__ in ('function', 'type'):
  14. return instance_or_class.__name__
  15. else:
  16. return instance_or_class.__class__.__name__
  17. def debug(self, instance_or_class, message):
  18. self._logger.debug(
  19. Logger.TPL.format(cls=self._txt(instance_or_class), msg=message)
  20. )
  21. def error(self, instance_or_class, message, exc_info=0):
  22. self._logger.error(
  23. Logger.TPL.format(
  24. cls=self._txt(instance_or_class),
  25. msg=message,
  26. exc_info=exc_info
  27. )
  28. )
  29. def info(self, instance_or_class, message):
  30. self._logger.info(
  31. Logger.TPL.format(cls=self._txt(instance_or_class), msg=message)
  32. )
  33. def warning(self, instance_or_class, message):
  34. self._logger.warning(
  35. Logger.TPL.format(cls=self._txt(instance_or_class), msg=message)
  36. )
  37. logger = Logger('tracim')