package cn.zhicall.web.aspect;
import com.zhicall.framework.core.common.utils.log.LogProxy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Component
@Aspect
public class RequestParamsAspect {
protected final Logger logger = LogProxy.getLogger("REQUEST_PARAM_LOG");
ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * cn.web.controller.*.*.*(..))")
public void pointCut() {}
@Before("pointCut()")
public void before(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("请求来源:" + request.getRemoteAddr());
logger.info("请求URL:" + request.getRequestURL().toString());
logger.info("请求方式:" + request.getMethod());
logger.info("响应方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("请求参数:" + Arrays.toString(joinPoint.getArgs()));
startTime.set(System.currentTimeMillis());
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object result = pjp.proceed();
logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));
return result;
}
@After("pointCut()")
public void after(JoinPoint point) {
if (startTime.get() == null) {
startTime.set(System.currentTimeMillis());
}
logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));
startTime.remove();
}
}