add: aop支持
This commit is contained in:
parent
702b679b84
commit
0866afbdbb
6
pom.xml
6
pom.xml
@ -128,6 +128,12 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--fastjson-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.35</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package cn.allms.aspect;
|
|||||||
|
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.*;
|
import org.aspectj.lang.annotation.*;
|
||||||
import org.hibernate.mapping.Join;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -13,23 +12,21 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Auther: 南迪叶先生:https://www.cnblogs.com/ye888/
|
* @author josxy
|
||||||
* @Date: 2019/9/26
|
|
||||||
* @Description: com.yrp.aspect
|
|
||||||
* @version: 1.0
|
|
||||||
*/
|
*/
|
||||||
@Aspect
|
|
||||||
@Component
|
@Component
|
||||||
public class LogAspect {
|
public class LogAspectOld {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
//拦截所要控制器
|
/**
|
||||||
@Pointcut("execution(* com.yrp.controller.*.*(..))")
|
* 切点:cn.allms.controller
|
||||||
|
*/
|
||||||
|
//@Pointcut("execution(* cn.allms.controller.*.*(..))")
|
||||||
public void log(){}
|
public void log(){}
|
||||||
|
|
||||||
|
|
||||||
@Before("log()")
|
// @Before("log()")
|
||||||
public void doBefore(JoinPoint joinPoint){
|
public void doBefore(JoinPoint joinPoint){
|
||||||
// 获取request对象
|
// 获取request对象
|
||||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
@ -47,12 +44,12 @@ public class LogAspect {
|
|||||||
logger.info("Request : {}",requestLog);
|
logger.info("Request : {}",requestLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After("log()")
|
//@After("log()")
|
||||||
public void doAfter(){
|
public void doAfter(){
|
||||||
// logger.info("-------doafter-----------");
|
// logger.info("-------doafter-----------");
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterReturning(returning = "result",pointcut = "log()")
|
//@AfterReturning(returning = "result",pointcut = "log()")
|
||||||
public void doAfterReturn(Object result){
|
public void doAfterReturn(Object result){
|
||||||
logger.info("控制器方法名称Result : {}",result);
|
logger.info("控制器方法名称Result : {}",result);
|
||||||
}
|
}
|
||||||
61
src/main/java/cn/allms/config/LogAspect.java
Normal file
61
src/main/java/cn/allms/config/LogAspect.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package cn.allms.config;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
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 rainerosion
|
||||||
|
* @date 2021/4/9 19:11
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@Aspect
|
||||||
|
public class LogAspect {
|
||||||
|
@Pointcut("execution(* cn.allms.controller.*.*(..))")
|
||||||
|
public void webLog(){}
|
||||||
|
|
||||||
|
@Before("webLog()")
|
||||||
|
public void doBefore(JoinPoint joinPoint) {
|
||||||
|
// 开始打印请求日志
|
||||||
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
|
||||||
|
// 打印请求相关参数
|
||||||
|
log.info("========================================== Start ==========================================");
|
||||||
|
// 打印请求 url
|
||||||
|
log.info("[请求URL] : {}", request.getRequestURL().toString());
|
||||||
|
// 打印 Http method
|
||||||
|
log.info("[请求方法] : {}", request.getMethod());
|
||||||
|
// 打印调用 controller 的全路径以及执行方法
|
||||||
|
log.info("[请求类名] : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
|
||||||
|
// 打印请求的 IP
|
||||||
|
log.info("[请求IP] : {}", request.getRemoteAddr());
|
||||||
|
// 打印请求入参
|
||||||
|
log.info("[请求参数] : {}", JSONObject.toJSONString(joinPoint.getArgs()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around("webLog()")
|
||||||
|
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
Object result = proceedingJoinPoint.proceed();
|
||||||
|
// 打印出参
|
||||||
|
log.info("[响应结果] : {}", JSONObject.toJSONString(result));
|
||||||
|
// 执行耗时
|
||||||
|
log.info("[请求耗时] : {} ms", System.currentTimeMillis() - startTime);
|
||||||
|
log.info("=========================================== End ===========================================");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user