引入相关的log4j2和aop的依赖包

打印请求和响应信息

@Aspect
@Component
public class WebLogAspect {
private final Logger log = LoggerFactory.getLogger(WebLogAspect.class);
@Pointcut("execution(public * com.imooc.mall.controller.*.*(..))")
public void webLog(){
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint){
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("CLASS_METHOD:"+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
log.info("ARGS:"+ Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "res",pointcut = "webLog()")
public void doAfterReturning(Object res) throws JsonProcessingException {
log.info("RESPONSE:"+ new ObjectMapper().writeValueAsString(res));
}
}
打印日志
