《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
/**
-
@description 拦截所有控制器的返回,记录响应报文
-
@author unhejing
-
@date 2021-03-17 上午11:52:26
*/
@Aspect
@Configuration
@Slf4j
public class LogAspect {
@Autowired
private LogFeignService logFeignService;
public LogAspect() {
}
@Pointcut(“execution(public * com.tjair.tjapi.controller..(…))”)
public void pointCutMethod() {
}
// 声明环绕通知
@Around(“pointCutMethod()”)
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
Long startTime = System.currentTimeMillis();
ApiOperation apiOperation = ((MethodSignature)pjp.getSignature()).getMethod().getAnnotation(ApiOperation.class);
// 获取request对象
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
Object ret = pjp.proceed();
Long endTime = System.currentTimeMillis();
TjapiLogVo tjapiLogVo = getTjapiLogVo(request,Objects.nonNull(apiOperation) ? apiOperation.value() : “”,endTime-startTime,JSON.toJSONString(ret));
// 添加日志
addLog(tjapiLogVo);
log.info(“响应数据耗时{}:响应数据{}”,endTime-startTime,JSON.toJSONString(ret));
return ret;
}
public void addLog(TjapiLogVo tjapiLogVo) {
log.info(“添加日志:{}”,JSON.toJSONString(tjapiLogVo));
Long startTime = System.currentTimeMillis();
logFeignService.addTjapiLog(tjapiLogVo);
Long endtime = System.currentTimeMillis();
log.info(“添加日志耗时:{}”,endtime-startTime);
}
private TjapiLogVo getTjapiLogVo(HttpServletRequest request,String apiName,Long time,String respons