// 定义一个切面
@Aspect
@Configuration
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);
// 定义切入点
@Pointcut("execution(* com.dome.spring.controller..*.*(..))")
public void controllerPointcut() {
}
// 定义事件触发节点,引用切入点
@Around("controllerPointcut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String url = request.getRequestURL().toString();
String method = request.getMethod();
Map<String, String> paramMap = new HashMap<>();
Enumeration<String> paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String name = paramNames.nextElement();
String value = request.getParameter(name);
paramMap.put(name, value);
}
logger.info("请求开始===地址:{}", url);
logger.info("请求开始===类型:{}", method);
logger.info("请求开始===参数:{}", JSON.toJSONString(paramMap));
// result的值就是被拦截方法的返回值
Object result = pjp.proceed();
logger.info("请求结束===返回值:{}", JSON.toJSONString(result));
return result;
}
}
Java切面的使用,记录请求日志
最新推荐文章于 2024-07-13 02:46:22 发布