您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

39 行
968B

  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(
  12. logging.Formatter(
  13. "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
  14. datefmt="%Y-%m-%d %H:%M:%S",
  15. )
  16. )
  17. file_handle = logging.FileHandler("run.log", encoding="utf-8")
  18. file_handle.setFormatter(
  19. logging.Formatter(
  20. "[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d] - %(message)s",
  21. datefmt="%Y-%m-%d %H:%M:%S",
  22. )
  23. )
  24. log.addHandler(file_handle)
  25. log.addHandler(console_handle)
  26. def _get_logger():
  27. log = logging.getLogger("log")
  28. _reset_logger(log)
  29. log.setLevel(logging.INFO)
  30. return log
  31. # 日志句柄
  32. logger = _get_logger()