logger.py 836 B

12345678910111213141516171819202122232425
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import time
  4. from loguru import logger
  5. # 日志文件配置
  6. # 本来是想 像flask那样把日志对象挂载到app对象上,作者建议直接使用全局对象
  7. # https://github.com/tiangolo/fastapi/issues/81#issuecomment-473677039
  8. # pip install loguru
  9. basedir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. # print(f"log basedir{basedir}") # /xxx/python_code/FastAdmin/backend/app
  11. # 定位到log日志文件
  12. log_path = os.path.join(basedir, 'logs')
  13. if not os.path.exists(log_path):
  14. os.mkdir(log_path)
  15. log_path_error = os.path.join(log_path, f'{time.strftime("%Y-%m-%d")}_error.log')
  16. # 日志简单配置
  17. # 具体其他配置 可自行参考 https://github.com/Delgan/loguru
  18. logger.add(log_path_error, rotation="00:00", retention="365 days", enqueue=True)