文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

SpringAOP实现接口请求记录到数据库的示例代码

2024-04-02 19:55

关注

1.引入AOP依赖

 <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>

2.创建日志记录表

DROP TABLE IF EXISTS `rule_operate_log`;
CREATE TABLE `rule_operate_log`  (
    id INT(11) NOT NULL AUTO_INCREMENT COMMENT '日志id',
    path VARCHAR(4000) NULL DEFAULT NULL COMMENT '接口地址',
    http_method VARCHAR(32) NULL DEFAULT NULL COMMENT '请求方法',
    status_code VARCHAR(32) NULL DEFAULT NULL COMMENT '请求返回状态码',
    create_time_char VARCHAR(32) NULL DEFAULT NULL COMMENT '日志时间',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '日志时间戳',
    ip varchar(200) NULL DEFAULT NULL COMMENT '请求ip',
    params mediumtext NULL COMMENT '请求参数',
    result mediumtext NULL COMMENT '返回值',
    exception mediumtext NULL COMMENT '接口异常',
    user_id VARCHAR(32) NULL DEFAULT NULL COMMENT '操作用户',
    user_account VARCHAR(32) NULL DEFAULT NULL COMMENT '操作用户账号',
    user_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作用户名称',
    user_org_id VARCHAR(32) NULL DEFAULT NULL COMMENT '操作用户机构id',
    user_org_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作用户机构名称',
    operate_name VARCHAR(200) NULL DEFAULT NULL COMMENT '操作名称',
    operate_position VARCHAR(200) NULL DEFAULT NULL COMMENT '操作位置',
    log_type VARCHAR(32) NULL DEFAULT NULL COMMENT '日志类型 error:错误日志 operate:操作日志',
    category_id VARCHAR(32) NULL DEFAULT NULL COMMENT '分类机构id',
    cost INT(11) NULL DEFAULT NULL COMMENT '接口耗时',
    PRIMARY KEY (id)
) COMMENT = '操作日志表';

3.日志实体类

import java.util.Date;

public class RuleOperateLog {
    
    private Integer id;
    
    private String path;
    
    private String httpMethod;
    
    private String statusCode;
    
    private String createTimeChar;
    
    private Date createTime;
    
    private String ip;
    
    private String params;
    
    private String result;
    
    private String exception;
    
    private String userId;
    
    private String userAccount;
    
    private String userName;
    
    private String userOrgId;
    
    private String userOrgName;
    
    private String operateName;
    
    private String operatePosition;
    
    private String logType;
    
    private String categoryId;
    
    private Integer cost;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getCreateTimeChar() {
        return createTimeChar;
    }

    public void setCreateTimeChar(String createTimeChar) {
        this.createTimeChar = createTimeChar;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(String userAccount) {
        this.userAccount = userAccount;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserOrgId() {
        return userOrgId;
    }

    public void setUserOrgId(String userOrgId) {
        this.userOrgId = userOrgId;
    }

    public String getUserOrgName() {
        return userOrgName;
    }

    public void setUserOrgName(String userOrgName) {
        this.userOrgName = userOrgName;
    }

    public String getOperateName() {
        return operateName;
    }

    public void setOperateName(String operateName) {
        this.operateName = operateName;
    }

    public String getOperatePosition() {
        return operatePosition;
    }

    public void setOperatePosition(String operatePosition) {
        this.operatePosition = operatePosition;
    }

    public String getLogType() {
        return logType;
    }

    public void setLogType(String logType) {
        this.logType = logType;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getCost() {
        return cost;
    }

    public void setCost(Integer cost) {
        this.cost = cost;
    }
}

4.Dao+Mapper+service

import com.xxx.xxx.xxx.entity.RuleOperateLog;


public interface RuleOperateLogDao {

    
    int insert(RuleOperateLog operateLog);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.xxx.xxx.dao.RuleOperateLogDao">

    <resultMap type="com.xxx.xxx.xxx.entity.RuleOperateLog" id="RuleOperateLogMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="path" column="path" jdbcType="VARCHAR"/>
        <result property="httpMethod" column="http_method" jdbcType="VARCHAR"/>
        <result property="statusCode" column="status_code" jdbcType="VARCHAR"/>
        <result property="createTimeChar" column="create_time_char" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="ip" column="ip" jdbcType="VARCHAR"/>
        <result property="params" column="params" jdbcType="VARCHAR"/>
        <result property="result" column="result" jdbcType="VARCHAR"/>
        <result property="exception" column="exception" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="VARCHAR"/>
        <result property="userAccount" column="user_account" jdbcType="VARCHAR"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userOrgId" column="user_org_id" jdbcType="VARCHAR"/>
        <result property="userOrgName" column="user_org_name" jdbcType="VARCHAR"/>
        <result property="operateName" column="operate_name" jdbcType="VARCHAR"/>
        <result property="operatePosition" column="operate_position" jdbcType="VARCHAR"/>
        <result property="logType" column="log_type" jdbcType="VARCHAR"/>
        <result property="categoryId" column="category_id" jdbcType="VARCHAR"/>
        <result property="cost" column="cost" jdbcType="INTEGER"/>
    </resultMap>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rule_operate_log (id, path, http_method, status_code, create_time_char, create_time,
                                      ip, params, result, exception, user_id, user_account, user_name, user_org_id,
                                      user_org_name, operate_name, operate_position, log_type, category_id, cost)
        values (#{id}, #{path}, #{httpMethod}, #{statusCode}, #{createTimeChar}, #{createTime}, #{ip}, #{params}, #{result},
                #{exception},#{userId}, #{userAccount}, #{userName}, #{userOrgId}, #{userOrgName}, #{operateName}, #{operatePosition},
                #{logType}, #{categoryId}, #{cost})
    </insert>

</mapper>
import com.xxx.xxx.xxx.entity.RuleOperateLog;


public interface RuleOperateLogService {

    
    void saveLog(RuleOperateLog ruleOperateLog);

}
import com.xxx.xxx.xxx.dao.RuleOperateLogDao;
import com.xxx.xxx.xxx.entity.RuleOperateLog;
import com.xxx.xxx.xxx.service.RuleOperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("RuleOperateLogService")
public class RuleOperateLogServiceImpl implements RuleOperateLogService {

    @Autowired
    private RuleOperateLogDao operateLogDao;

    @Override
    public void saveLog(RuleOperateLog ruleOperateLog) {
        operateLogDao.insert(ruleOperateLog);
    }
}

5.自定义注解

import java.lang.annotation.*;

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LogResource {
    
    String name();

    
    String position() default "";

    
    String logType() default "";
}

6.操作日志切面类

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.com.xxx.xxx.xxx.annotation.LogResource;
import com.com.xxx.xxx.xxx.constants.LogTypeConstants;
import com.com.xxx.xxx.xxx.entity.RuleOperateLog;
import com.com.xxx.xxx.xxx.service.RuleOperateLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;



@Aspect
@Component
public class OperateLogAspect {

    @Autowired
    private RuleOperateLogService operateLogService;

    //扫描使用@LogResource注解的方法
    @Pointcut("@annotation(com.com.xxx.xxx.xxx.annotation.LogResource)")
    public void logPointCut() { };

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Date startTime = new Date();
        String exception = null;
        String result = null;
        try {
            Object obj = point.proceed();
            if (obj != null) {
                result = JSONObject.toJSONString(obj);
            }
            return obj;
        } catch (Exception e) {
            //请求时报错
            exception = e.toString();
            throw e;
        } finally {
            //操作和报错日志都记录
            HttpServletResponse response
                    = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            int statusCode = response.getStatus();
            if (exception != null) {
                
                statusCode = 500;
                
            }
            syncSaveLog(point, startTime, new Date(), exception, result, statusCode);
        }
    }

    @Async
    void syncSaveLog(ProceedingJoinPoint joinPoint, Date startTime, Date endTime,
                     String exception, String result, int statusCode) {
        RuleOperateLog log = new RuleOperateLog();
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            LogResource annotation = method.getAnnotation(LogResource.class);
            if (annotation != null) {
                //注解上的描述
                log.setOperateName(annotation.name());
            }
            Date nowDate = new Date();
            log.setCreateTimeChar(new SimpleDateFormat("yyyyMMddhhmmss").format(nowDate));
            log.setCreateTime(nowDate);
            //入参
            if (joinPoint.getArgs() != null) {
                try {
                    log.setParams(JSONObject.toJSONString(joinPoint.getArgs(),
                            SerializerFeature.IgnoreNonFieldGetter));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            Long cost = endTime.getTime() - startTime.getTime();
            log.setCost(cost.intValue());
            HttpServletRequest request
                    = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            if (request != null) {
                log.setUserName(request.getHeader(HttpHeaders.USER_AGENT));
                log.setPath(request.getRequestURI());
                log.setHttpMethod(request.getMethod());
                log.setIp(request.getRemoteAddr());
            }
            log.setStatusCode(String.valueOf(statusCode));
            log.setResult(result);
            
            if (statusCode > 400 && exception != null) {
                log.setException(exception);
                log.setLogType(LogTypeConstants.ERROR);
            } else {
                log.setLogType(LogTypeConstants.OPERATE);
            }
            
            operateLogService.saveLog(log);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

7.使用

到此这篇关于Spring AOP实现接口请求记录到数据库的文章就介绍到这了,更多相关Spring AOP接口请求记录数据库内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯