lemon_logger.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import logging
  2. def singleton(cls):
  3. instances = {}
  4. def _singleton(*args,**kwargs):
  5. if cls not in instances:
  6. instances[cls] = cls(*args,**kwargs)
  7. return instances[cls]
  8. return _singleton
  9. @singleton
  10. class Logger(object):
  11. level_relations = {
  12. 'debug':logging.DEBUG,
  13. 'info':logging.INFO,
  14. 'warning':logging.WARNING,
  15. 'error':logging.ERROR,
  16. 'critical':logging.CRITICAL
  17. }
  18. def __init__(self,level='info',when='D',backCount=3,fmt='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s'):
  19. self.logger = logging.getLogger()
  20. format_str = logging.Formatter(fmt)
  21. self.logger.setLevel(self.level_relations.get(level))
  22. if not self.logger.handlers:
  23. sh = logging.StreamHandler()
  24. sh.setFormatter(format_str)
  25. self.logger.addHandler(sh)
  26. def info(self,msg):
  27. self.logger.info(msg=msg)
  28. def error(self,msg):
  29. self.logger.error(msg=msg)
  30. def warning(self,msg):
  31. self.logger.warning(msg=msg)
  32. def exception(self,msg,exc_info=True):
  33. self.logger.exception(msg=msg,exc_info=exc_info)