log.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import logging
  2. from logging import handlers
  3. from app.app_config import PER_LOG_FILESIZE,LOG_FILE_NUM,LOG_PATH
  4. """
  5. desc:
  6. 系统日志类
  7. """
  8. class Logger(object):
  9. level_relations = {
  10. 'debug':logging.DEBUG,
  11. 'info':logging.INFO,
  12. 'warning':logging.WARNING,
  13. 'error':logging.ERROR,
  14. 'crit':logging.CRITICAL
  15. }
  16. def __init__(self,filename = LOG_PATH, level = 'info', when = 'D',backCount = LOG_FILE_NUM, \
  17. fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', filesize = PER_LOG_FILESIZE):
  18. self.logger = logging.getLogger(filename)
  19. format_str = logging.Formatter(fmt)
  20. self.logger.setLevel(self.level_relations.get(level))
  21. sh = logging.StreamHandler()
  22. sh.setFormatter(format_str)
  23. th = handlers.RotatingFileHandler(
  24. filename = filename,
  25. maxBytes=filesize,
  26. backupCount=backCount,
  27. encoding = 'utf-8')
  28. th.setFormatter(format_str)
  29. self.logger.addHandler(sh)
  30. self.logger.addHandler(th)