@Aspect
@Component
@Slf4j
public class ReqResAop {
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)" +
"||@annotation(org.springframework.web.bind.annotation.GetMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PostMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PutMapping)" +
"||@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void performance() {
}
@Around("performance()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long start = System.currentTimeMillis();
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
String url = request.getRequestURI();
Object[] objects = proceedingJoinPoint.getArgs();
Object result = null;
try {
String reqStr = JsonUtils.obj2Json(objects);
result = proceedingJoinPoint.proceed();// result的值就是被拦截方法的返回值
String resStr = JsonUtils.obj2Json(result);
long end = System.currentTimeMillis();
log.info("\n===============请求===============\n" +
"url:{}\n" +
"{}\n" +
"===============响应===============\n" +
"{}\n" +
"=============耗时:{} 毫秒============================"
, url, reqStr, resStr, (end - start));
log.info("===============END===============");
} catch (Exception e) {
log.error(e.getMessage(), e);
//这里不抛出异常的话,RestControllerAdvice就拦截不到了,因为此处捕获了
throw new RuntimeException(e.getMessage());
}
return result;
}
}
AOP记录Controller日志
猜你喜欢
转载自blog.csdn.net/m0_37208669/article/details/84799496
今日推荐
周排行