springboot集成aop日志
日常开发中假如是前后端完全分离,我们会习惯用浏览器去调用controller的接口来测试。这一个过程普通的日志功能会记录sql参数等一些基本信息。但是假如项目越来越庞大,我们的包越来越多,在维护项目和二次开发中去调用页面的相关接口后去寻找相关接口的包或者类就会有一定困难,我们可以通过aop去记录一次请求过程中的类名,方法名,请求url等。
代码
引入相关依赖
这里直接引入和aop相关的pom
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.15</version>
</dependency>
切面类
先看一下包结构
在之前的项目基础上创建aop包
然后建立切面类LogAspect
package com.ccsert.aop;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @author ccsert
* @version V1.0
* @Description: TODO
* @date
*/
@Aspect
@Component
public class LogAspect {
private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);
//这里考虑到项目层次关系根据自己的项目去写表达式,此处是监测demo下所有的包下的controller包下以及其子包的类
@Pointcut("execution(public * com.ccsert.demo.*.controller..*.*(..))")
public void controllerMethod() {
}
@Before("controllerMethod()")
public void LogRequestInfo(JoinPoint joinPoint) throws Exception {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
StringBuffer requestLog = new StringBuffer();
requestLog.append("请求信息:")
.append("URL = {" + request.getRequestURI() + "},\t")
.append("HTTP_METHOD = {" + request.getMethod() + "},\t")
.append("IP = {" + request.getRemoteAddr() + "},\t")
.append("CLASS_METHOD = {" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "},\t");
if (joinPoint.getArgs().length == 0) {
requestLog.append("ARGS = {} ");
} else {
requestLog.append("ARGS = " + new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)
.writeValueAsString(joinPoint.getArgs()[0]) + "");
}
logger.info(requestLog.toString());
}
}
其实这样基本就完事了
Controler类
package com.ccsert.demo.login.controller;
import com.ccsert.demo.login.model.UserInfo;
import com.ccsert.demo.login.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author ccsert
* @version V1.0
* @Description: TODO
* @date
*/
@RestController
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
@RequestMapping("getall")
public List<UserInfo> selectAll(){
List<UserInfo> userInfos = userInfoService.selectAll();
return userInfos;
}
}
我们用上次写的controller进行测试
测试
在浏览器访问接口
2019-02-13 17:40:14.943 INFO 23672 --- [nio-8080-exec-5] com.ccsert.aop.LogAspect : 请求信息:URL = {/user/getall}, HTTP_METHOD = {GET}, IP = {0:0:0:0:0:0:0:1}, CLASS_METHOD = {com.ccsert.demo.login.controller.UserInfoController.selectAll}, ARGS = {}
2019-02-13 17:40:14.944 DEBUG 23672 --- [nio-8080-exec-5] c.c.d.l.dao.UserInfoMapper.selectAll : ==> Preparing: SELECT id,user_name,password FROM user_info
2019-02-13 17:40:14.944 DEBUG 23672 --- [nio-8080-exec-5] c.c.d.l.dao.UserInfoMapper.selectAll : ==> Parameters:
2019-02-13 17:40:14.948 DEBUG 23672 --- [nio-8080-exec-5] c.c.d.l.dao.UserInfoMapper.selectAll : <== Total: 4
我们看见控制台成功记录了这次请求的请求url,请求方式,ip,以及完整的方法名
这样我们就整合是aop的日志功能更
想学习交流的可以加我的群 562942503