一、切面
@Slf4j
@Aspect
@Component
public class LogAspect {
private static final Integer WARN_TIME = 3000;
private static final Integer ERROR_TIME = 10000;
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restControllerLogAspect() {
}
@Around("restControllerLogAspect()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// request请求
HttpServletRequest request = (attributes == null ? null : attributes.getRequest());
// 请求url
String requestUrl = request == null ? "" : request.getRequestURL().toString();
// http方法(post/get)
String method = request == null ? "" : request.getMethod();
// 请求的类
String className = pjp.getSignature().getDeclaringTypeName();
// 请求的方法
String functionName = pjp.getSignature().getName();
// 打印json类型的入参
if (!ObjectUtils.isEmpty(request.getHeader(HttpHeaders.CONTENT_TYPE)) && request.getHeader(HttpHeaders.CONTENT_TYPE).startsWith(MediaType.APPLICATION_JSON_VALUE)) {
String args = JSON.toJSONString(pjp.getArgs());
log.info("请求url:{}, http方法:{}, 类.方法:{}.{}, 入参:{}", requestUrl, method, className, functionName, args);
} else {
log.info("请求url:{}, http方法:{}, 类.方法:{}.{}", requestUrl, method, className, functionName);
}
// 开始时间
long startTime = System.currentTimeMillis();
try {
Object object = proceedingJoinPoint.proceed();
log.info("[请求切面] 正常结束 响应结果 = {}", JsonUtil.bean2JsonString(object));
return object;
} catch (Exception e) {
log.error("[请求切面] 异常", e);
throw e;
} finally {
Long time = System.currentTimeMillis() - startTime;
if (time > ERROR_TIME) {
log.error("[请求切面] 时间超长错误: used time is {}ms", time);
} else if (time > WARN_TIME) {
log.warn("[请求切面] 时间超长警告: used time is {}ms ", time);
} else {
log.info("[请求切面] 正常结束: used time is {}ms ", time);
}
}
}
}
二、为什么要针对文件流进行过滤?
因为:
如果对流文件进行参数打印,执行代码:JSON.toJSONString(pjp.getArgs()); 会报错:
MultipartFile resource [uploadFile] cannot be resolved to absolute file path