Commit ed3c80ca by 丁伟峰

请求应答报文日志打印

1 parent 0f009156
package com.alibaba.cloud.integration.order;
import com.alibaba.cloud.integration.order.interceptor.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableJpaRepositories
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private RequestInterceptor requestInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestInterceptor).addPathPatterns("/**");
}
}
package com.alibaba.cloud.integration.order.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Aspect
@Component
public class RequestInterceptor implements HandlerInterceptor {
/**
* 以 controller 包下定义的所有请求为切入点
*/
@Pointcut(value = "execution(public * com.alibaba.cloud.integration.order.controller..*.*(..))")
public void reqOpenAPILog() {
}
/**
* 在切点之前织入
*
* @param joinPoint
* @throws Throwable
*/
@Before("reqOpenAPILog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 开始打印请求日志
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 打印请求 url
log.info("Request URL : {}", request.getRequestURL().toString());
// 打印 Http method
log.info("HTTP Method : {}", request.getMethod());
// 打印调用 controller 的全路径以及执行方法
log.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// 打印请求的 IP
log.info("Request IP : {}", request.getRemoteAddr());
// 打印请求入参
log.info("Request Args : {}", joinPoint.getArgs());
}
/**
* 在切点之后织入
*
* @throws Throwable
*/
@After("reqOpenAPILog()")
public void doAfter() throws Throwable {
// TODO
}
/**
* 环绕
*
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("reqOpenAPILog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("========================================== Start ==========================================");
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
// 打印出参
log.info("Response Args : {}", result);
// 执行耗时
log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
log.info("=========================================== End ===========================================");
return result;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!