You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829
  1. import logging
  2. import sys
  3. def _reset_logger(log):
  4. for handler in log.handlers:
  5. handler.close()
  6. log.removeHandler(handler)
  7. del handler
  8. log.handlers.clear()
  9. log.propagate = False
  10. console_handle = logging.StreamHandler(sys.stdout)
  11. console_handle.setFormatter(logging.Formatter('[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s',
  12. datefmt='%Y-%m-%d %H:%M:%S'))
  13. file_handle = logging.FileHandler('run.log', encoding='utf-8')
  14. file_handle.setFormatter(logging.Formatter('[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s',
  15. datefmt='%Y-%m-%d %H:%M:%S'))
  16. log.addHandler(file_handle)
  17. log.addHandler(console_handle)
  18. def _get_logger():
  19. log = logging.getLogger('log')
  20. _reset_logger(log)
  21. log.setLevel(logging.INFO)
  22. return log
  23. # 日志句柄
  24. logger = _get_logger()