""" 系统日志模型 用于记录系统操作、错误、安全事件等日志信息 """ from datetime import datetime from sqlalchemy import Column, Integer, String, Text, DateTime, Index from sqlalchemy.orm import Mapped, mapped_column from app.models.base import BaseModel class SystemLog(BaseModel): """ 系统日志模型 记录系统各类操作日志 """ __tablename__ = "system_logs" # 日志级别: debug, info, warning, error level: Mapped[str] = mapped_column(String(20), nullable=False, index=True) # 日志类型: system, user, api, error, security type: Mapped[str] = mapped_column(String(50), nullable=False, index=True) # 操作用户(可能为空,如系统自动操作) user: Mapped[str] = mapped_column(String(100), nullable=True, index=True) # 用户ID(可能为空) user_id: Mapped[int] = mapped_column(Integer, nullable=True, index=True) # IP地址 ip: Mapped[str] = mapped_column(String(100), nullable=True) # 日志消息 message: Mapped[str] = mapped_column(Text, nullable=False) # User Agent user_agent: Mapped[str] = mapped_column(String(500), nullable=True) # 请求路径(API路径) path: Mapped[str] = mapped_column(String(500), nullable=True, index=True) # 请求方法 method: Mapped[str] = mapped_column(String(10), nullable=True) # 额外数据(JSON格式,可存储详细信息) extra_data: Mapped[str] = mapped_column(Text, nullable=True) # 创建索引以优化查询性能 __table_args__ = ( Index('idx_system_logs_created_at', 'created_at'), Index('idx_system_logs_level_type', 'level', 'type'), Index('idx_system_logs_user_created', 'user', 'created_at'), ) def __repr__(self): return f""