Log.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. '''
  2. Log类
  3. '''
  4. __author__ = 'Wang Liming'
  5. import logging
  6. import os
  7. class Mylog:
  8. def __init__(self, log_name='this_log', log_level = ''):
  9. self.logger = logging.getLogger(log_name)
  10. self._set_log_level(logging.INFO)
  11. if len(log_level) > 0:
  12. self._set_log_level(log_level)
  13. def get_logger(self):
  14. return self.logger
  15. def set_file_hl(self, file_name='all.log', log_level='info'):
  16. fh = logging.FileHandler(filename=file_name)
  17. fh_formatter = logging.Formatter('%(asctime)s:%(created)f:%(name)s:%(module)s:%(funcName)s:%(levelname)s:%(message)s')
  18. fh.setFormatter(fh_formatter)
  19. if len(log_level) > 0:
  20. self._set_log_level(log_level)
  21. self.logger.addHandler(fh)
  22. def set_stream_hl(self, log_level='info'):
  23. sh = logging.StreamHandler()
  24. sh_formatter = logging.Formatter('%(asctime)s:%(created)f:%(name)s:%(module)s:%(funcName)s:%(levelname)s:%(message)s')
  25. sh.setFormatter(sh_formatter)
  26. if len(log_level) > 0:
  27. self._set_log_level(log_level)
  28. self.logger.addHandler(sh)
  29. def _set_log_level(self, log_level):
  30. if log_level == 'debug':
  31. self.logger.setLevel(logging.DEBUG)
  32. if log_level == 'info':
  33. self.logger.setLevel(logging.INFO)
  34. if log_level == 'warning':
  35. self.logger.setLevel(logging.WARNING)
  36. if log_level == 'error':
  37. self.logger.setLevel(logging.ERROR)