api_log.py 1.3 KB

123456789101112131415161718192021222324252627
  1. """
  2. API日志模型
  3. """
  4. from datetime import datetime
  5. from app.utils.timezone import beijing_now_naive
  6. from sqlalchemy import Column, Integer, String, DateTime, Text
  7. from app.database import Base
  8. class APILog(Base):
  9. """API请求日志表"""
  10. __tablename__ = "api_logs"
  11. id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
  12. user_id = Column(Integer, nullable=True, comment="用户ID(可为NULL)")
  13. endpoint = Column(String(255), nullable=False, comment="接口路径")
  14. method = Column(String(10), nullable=False, comment="HTTP方法")
  15. request_data = Column(Text, nullable=True, comment="加密前的原始请求参数")
  16. response_data = Column(Text, nullable=True, comment="解密后的原始响应数据")
  17. ip_address = Column(String(45), nullable=True, comment="客户端IP")
  18. user_agent = Column(String(500), nullable=True, comment="用户代理")
  19. response_code = Column(Integer, nullable=True, comment="响应状态码")
  20. execution_time = Column(Integer, nullable=True, comment="执行时间(毫秒)")
  21. created_at = Column(DateTime, default=beijing_now_naive, comment="创建时间")
  22. def __repr__(self):
  23. return f"<APILog(id={self.id}, endpoint='{self.endpoint}', method='{self.method}')>"