1. 程式人生 > >springboot AOP全局攔截日誌記錄

springboot AOP全局攔截日誌記錄

quest com info throws ret https log element aspect

@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());
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("#######################請求結束#############################");

}


}

springboot AOP全局攔截日誌記錄