springboot AOP全局拦截日志记录

时间:2022-01-18 03:23:04
@Aspect
@Component
@Slf4j
public class WebLogAspect {
@Pointcut("execution(public * com.we.controller.*.*(..))")
public void webLog(){
} @Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
log.info("#######################请求开始#############################");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("URL:"+request.getRequestURL().toString());
log.info("HTTP_METHOD:"+request.getMethod());
log.info("IP:"+request.getRemoteAddr());
log.info("HTTPparameters:"+JSON.toJSONString(joinPoint.getArgs());
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String name = enu.nextElement();
log.info("name:{},value:{}",name,request.getParameter(name));
}
} @AfterReturning(returning ="ret",pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable{
log.info("Response:"+ret);
log.info("#######################请求结束#############################");
} }