添加aop类
parent
df447b2439
commit
2e77996062
|
@ -0,0 +1,84 @@
|
|||
package com.fate.firm.aop;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.ServletRegistration;
|
||||
import javax.servlet.ServletRequestAttributeEvent;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @description: TODO
|
||||
* @author: SIKADI
|
||||
* @date: 2023/11/22 10:56
|
||||
**/
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class Aop {
|
||||
// 日志打印
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 定义切面的那个类
|
||||
* @author: ZhuoXin
|
||||
* @date: 2023/11/22 11:02
|
||||
* @param: []
|
||||
* @return: void
|
||||
**/
|
||||
@Pointcut("execution(* com.fate.firm.controller.*.*(..))")
|
||||
public void print(){};
|
||||
|
||||
@Around("print()")
|
||||
public Object LogStart(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
// 使用ServletRequestAttributes请求上下文获取更多
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
String declaringTypeName = proceedingJoinPoint.getSignature().getDeclaringTypeName();
|
||||
String methodName = proceedingJoinPoint.getSignature().getName();
|
||||
// 使用数据来获取参数
|
||||
Object[] args = proceedingJoinPoint.getArgs();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
logger.info("—_—方—_—_—法_—_—_—启—_—_—动—_—_—开—_—_—始—_—_—希—_—_—望—_—_—无—_—_—B—_—_—U—_—_—G—_—");
|
||||
logger.info("|调用前时 :{}",declaringTypeName);
|
||||
logger.info("|方法名称 :{}",methodName);
|
||||
logger.info("|传递参数 :{}",objectMapper.writeValueAsString(objectMapper));
|
||||
logger.info("|URL :{}",request.getRequestURI().toString());
|
||||
logger.info("|IP :{}",request.getRemoteAddr());
|
||||
logger.info("————————————————————————————————————————————————————————————————————————————————");
|
||||
|
||||
|
||||
Object proceed = proceedingJoinPoint.proceed();
|
||||
logger.info("—_—方—_—_—法_—_—_—结—_—_—束—_—_—开—_—_—始—_—_—希—_—_—望—_—_—无—_—_—B—_—_—U—_—_—G—_—");
|
||||
logger.info("|调用后时 :{}",declaringTypeName);
|
||||
logger.info("|方法名称 :{}",methodName);
|
||||
logger.info("|接收参数 :{}",objectMapper.writeValueAsString(proceed));
|
||||
logger.info("|URL :{}",request.getRequestURI().toString());
|
||||
logger.info("|IP :{}",request.getRemoteAddr());
|
||||
logger.info("|耗时 :{}ms",System.currentTimeMillis() - startTime.get());
|
||||
logger.info("————————————————————————————————————————————————————————————————————————————————");
|
||||
|
||||
|
||||
return proceed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue