引入依赖
<!-- 引入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); } }
存入数据库步骤省略