springboot AOP全局拦截日志记录

@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("#######################请求结束#############################");
} }
上一篇:Spring 4 bak


下一篇:《Linux内核设计与实现》 第八周读书笔记 第四章 进程调度