@Slf4j
@Aspect
@Component
public class RequestLogAspect {
@Around("execution(* com.gegeda.sp.controller..*.* (..))")
public Object around(ProceedingJoinPoint jp) throws Throwable {
HttpServletRequest request = WebUtils.getRequest();
if (request == null) {
return jp.proceed();
}
String uri = request.getRequestURI();
String dataKey = WebUtils.getDataKey();
log.info("-------------------------------------------------来自租户{}的请求{}开始-------------------------------------------------", dataKey, uri);
MethodSignature signature = (MethodSignature) jp.getSignature();
log.info("请求执行方法:{}", signature.getMethod().getDeclaringClass().getName() + "." + signature.getMethod().getName());
Object[] params = jp.getArgs();
log.info("请求参数:{}", JSON.toJSONString(params));
long start = System.currentTimeMillis();
Object proceed = jp.proceed();
long end = System.currentTimeMillis();
long runtime = end - start;
if (runtime > 200) {
log.warn("-------------------------------------------------来自租户{}请求{}结束, 耗时 {}ms-------------------------------------------------", dataKey, uri, runtime);
} else {
log.info("-------------------------------------------------来自租户{}请求{}结束, 耗时{}ms-------------------------------------------------", dataKey, uri, runtime);
}
return proceed;
}
}
可以在代码中增加排除逻辑, 例如放行get请求, 其他请求都记录
还可以增加持久化方案保存到数据库, 但是不太建议直接保存到数据库, 只是日志而且会明显加了一个没有任何业务的代码, 想过一个方案, 存redis, 设置一个上限数据, 什么时候达到这个上限什么时候保存到数据库或者直接写execl