il y a 2 ans
il y a 2 ans
il y a 1 an
il y a 2 ans
il y a 1 an
il y a 1 an
il y a 2 ans
il y a 1 an
1234567891011121314151617181920212223242526272829303132333435363738
  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()