SpringBoot 使用 AOP 打印请求日志
200
2017-06-10
@Aspect
@Configuration
public class WebServiceLogger {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/*
* 定义一个切入点
*/
@Pointcut("execution(* com.example.service.GirlService.*(..))")
public void excudeService(){}
/*
* 通过连接点切入
*/
@Before("excudeService()")
public void doBefore(JoinPoint joinPoint){
// 接收到请求,记录请求内容
logger.info("WebLogAspect.doBefore()");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("URL : " + request.getRequestURL().toString()); // 请求路径
logger.info("HTTP_METHOD : " + request.getMethod()); // 请求方法 get、post
logger.info("IP : " + request.getRemoteAddr()); // 请求者IP
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); //触发事务的方法
logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//获取所有参数并输出到控制台:
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=(String)enu.nextElement();
System.out.println(paraName+": "+request.getParameter(paraName));
}
}
@AfterReturning("excudeService()")
public void doAfterReturning(JoinPoint joinPoint){
// 处理完请求,返回内容
logger.info("WebLogAspect.doAfterReturning()");
}
}
- 0
- 0
-
分享