spring boot aop记录接口调用情况

引入依赖

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

定义日志实体


import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;

@Data
@TableName("t_cmp_interlog")
public class InterFaceLog {

    @TableField("url")
    private String url;
    @TableField("method")
    private String method;
    @TableField("param")
    private String param;
    @TableField("callTm")
    private String callTm;
    @TableField("callerID")
    private Integer callerID;
    @TableField("callerAcc")
    private String callerAcc;
    @TableField("time")
    private Long time;
    @TableField("id")
    private String id;
    @TableField("remake")
    private String remake;
    @TableField("remoteAddr")
    private String remoteAddr;
    @TableField("class_method")
    private String class_method;
    @TableField("result")
    private String result;

}

切面实现日志记录,切面在controller层


import cn.com.suntree.cmp.common.ApiResult;
import cn.com.suntree.cmp.entity.InterFaceLog;
import cn.com.suntree.cmp.entity.SysUser;
import cn.com.suntree.cmp.service.InterFaceLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.Arrays;
import java.util.Date;
import java.util.UUID;

@Aspect
@Component
public class WebLogAspect {
    @Autowired
    InterFaceLogService logService;

    ThreadLocal<Long> startTime = new ThreadLocal<>();
    ThreadLocal<InterFaceLog> log = new ThreadLocal<>();
    InterFaceLog interfaceLog =  new InterFaceLog();

    @Pointcut("execution(public * cn.com.suntree.cmp.controller.*.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        startTime.set(System.currentTimeMillis());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String remoteAddr = request.getRemoteAddr();
        String class_method = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        String param = Arrays.toString(joinPoint.getArgs());

        SysUser loginUser = CommonUtil.getLoginUser();
        if (!CommonUtil.check(loginUser)){
            return;
        }
        interfaceLog.setCallerAcc(loginUser.getAccNum());
        interfaceLog.setCallerID(loginUser.getUserID());
        interfaceLog.setClass_method(class_method);
        interfaceLog.setMethod(method);
        interfaceLog.setUrl(url);
        interfaceLog.setRemoteAddr(remoteAddr);
        interfaceLog.setParam(param);
        log.set(interfaceLog);
    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
    String result = ret.toString();
        InterFaceLog logAfter =  log.get();
        Long time = System.currentTimeMillis() - startTime.get();
        logAfter.setResult(result);
        logAfter.setCallTm(DateUtil.dateToYMDHMS(new Date()));
        logAfter.setTime(time);
        logAfter.setId(UUID.randomUUID().toString());
        ApiResult apiResult = logService.addLog(logAfter);
        System.out.println("apiResult ------------ "+apiResult);
        System.out.println(logAfter);
    }
}

存入数据库步骤省略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值