因为采取数据库读写分离,java服务端没有写权限,为了方便查看接口请求,做了一个拦截器,用来获取Controller(请求url、ip、参数、方法名、耗时)并日志打印,有兴趣也可以打印返回数据(不过数据量太多没写了),加上ret打印就行
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* @Description:服务切面
* @Author: lifay
* @datetime:2019/5/23 10:54
*
*/
@Aspect
@Component
public class ServerAspect {
private final Logger logger = LoggerFactory.getLogger(ServerAspect.class);
@Around("execution(public * com.dx.controller..*.*(..))")
public Object controllerLog(ProceedingJoinPoint pdj) throws Throwable{
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
long startTime = System.currentTimeMillis();//开始时间
Object[] args = pdj.getArgs();//获取参数数组
Object ret = pdj.proceed(args);//执行原方法并获取返回结果
long endTime = System.currentTimeMillis();//结束时间
// 记录下请求内容
StringBuilder sb = new StringBuilder();
sb.append("###请求URL: " + request.getRequestURL().toString());
sb.append(" ###IP: " + request.getRemoteAddr());
sb.append(" ###Params: " + Arrays.toString(args));
sb.append(" ###CLASS_METHOD: " + pdj.getSignature().getDeclaringTypeName() + "." + pdj.getSignature().getName());
sb.append(" ###耗时: " + (endTime - startTime) + "毫秒");
logger.info(sb.toString());
return ret;
}
}