2018-12-24 11:58:45 +08:00
|
|
|
|
package org.springblade.core.boot.logger;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
|
import org.springblade.core.tool.jackson.JsonUtil;
|
|
|
|
|
import org.springblade.core.tool.utils.BeanUtil;
|
|
|
|
|
import org.springblade.core.tool.utils.ClassUtil;
|
|
|
|
|
import org.springblade.core.tool.utils.StringUtil;
|
|
|
|
|
import org.springblade.core.tool.utils.WebUtil;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
|
|
import org.springframework.core.MethodParameter;
|
|
|
|
|
import org.springframework.core.io.InputStreamSource;
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.context.request.WebRequest;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spring boot 控制器 请求日志,方便代码调试
|
|
|
|
|
*
|
|
|
|
|
* @author L.cm
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Aspect
|
|
|
|
|
@Configuration
|
|
|
|
|
@Profile({"dev", "test"})
|
|
|
|
|
public class RequestLogAspect {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AOP 环切 控制器 R 返回值
|
2018-12-27 13:29:11 +08:00
|
|
|
|
*
|
2018-12-24 11:58:45 +08:00
|
|
|
|
* @param point JoinPoint
|
|
|
|
|
* @return Object
|
2018-12-27 13:29:11 +08:00
|
|
|
|
* @throws Throwable 异常
|
2018-12-24 11:58:45 +08:00
|
|
|
|
*/
|
|
|
|
|
@Around(
|
2019-01-21 17:48:03 +08:00
|
|
|
|
"execution(!static org.springblade.core.tool.api.R *(..)) && " +
|
2018-12-24 11:58:45 +08:00
|
|
|
|
"(@within(org.springframework.stereotype.Controller) || " +
|
|
|
|
|
"@within(org.springframework.web.bind.annotation.RestController))"
|
|
|
|
|
)
|
|
|
|
|
public Object aroundApi(ProceedingJoinPoint point) throws Throwable {
|
|
|
|
|
MethodSignature ms = (MethodSignature) point.getSignature();
|
|
|
|
|
Method method = ms.getMethod();
|
|
|
|
|
Object[] args = point.getArgs();
|
|
|
|
|
final Map<String, Object> paraMap = new HashMap<>(16);
|
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
|
MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
|
|
|
|
|
PathVariable pathVariable = methodParam.getParameterAnnotation(PathVariable.class);
|
|
|
|
|
if (pathVariable != null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
RequestBody requestBody = methodParam.getParameterAnnotation(RequestBody.class);
|
|
|
|
|
Object object = args[i];
|
|
|
|
|
// 如果是body的json则是对象
|
|
|
|
|
if (requestBody != null && object != null) {
|
|
|
|
|
paraMap.putAll(BeanUtil.toMap(object));
|
|
|
|
|
} else {
|
|
|
|
|
RequestParam requestParam = methodParam.getParameterAnnotation(RequestParam.class);
|
|
|
|
|
String paraName;
|
|
|
|
|
if (requestParam != null && StringUtil.isNotBlank(requestParam.value())) {
|
|
|
|
|
paraName = requestParam.value();
|
|
|
|
|
} else {
|
|
|
|
|
paraName = methodParam.getParameterName();
|
|
|
|
|
}
|
|
|
|
|
paraMap.put(paraName, object);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HttpServletRequest request = WebUtil.getRequest();
|
|
|
|
|
String requestURI = request.getRequestURI();
|
|
|
|
|
String requestMethod = request.getMethod();
|
|
|
|
|
// 处理 参数
|
|
|
|
|
List<String> needRemoveKeys = new ArrayList<>(paraMap.size());
|
|
|
|
|
paraMap.forEach((key, value) -> {
|
|
|
|
|
if (value instanceof HttpServletRequest) {
|
|
|
|
|
needRemoveKeys.add(key);
|
|
|
|
|
paraMap.putAll(((HttpServletRequest) value).getParameterMap());
|
|
|
|
|
} else if (value instanceof HttpServletResponse) {
|
|
|
|
|
needRemoveKeys.add(key);
|
|
|
|
|
} else if (value instanceof InputStream) {
|
|
|
|
|
needRemoveKeys.add(key);
|
|
|
|
|
} else if (value instanceof MultipartFile) {
|
|
|
|
|
String fileName = ((MultipartFile) value).getOriginalFilename();
|
|
|
|
|
paraMap.put(key, fileName);
|
|
|
|
|
} else if (value instanceof InputStreamSource) {
|
|
|
|
|
needRemoveKeys.add(key);
|
|
|
|
|
} else if (value instanceof WebRequest) {
|
|
|
|
|
needRemoveKeys.add(key);
|
|
|
|
|
paraMap.putAll(((WebRequest) value).getParameterMap());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
needRemoveKeys.forEach(paraMap::remove);
|
2019-02-27 23:05:45 +08:00
|
|
|
|
|
|
|
|
|
// 构建成一条长 日志,避免并发下日志错乱
|
|
|
|
|
StringBuilder logBuilder = new StringBuilder(500);
|
|
|
|
|
// 日志参数
|
|
|
|
|
List<Object> logArgs = new ArrayList<>();
|
|
|
|
|
logBuilder.append("\n\n================ Request Start ================\n");
|
2018-12-24 11:58:45 +08:00
|
|
|
|
// 打印请求
|
|
|
|
|
if (paraMap.isEmpty()) {
|
2019-02-27 23:05:45 +08:00
|
|
|
|
logBuilder.append("===> {}: {}\n");
|
|
|
|
|
logArgs.add(requestMethod);
|
|
|
|
|
logArgs.add(requestURI);
|
2018-12-24 11:58:45 +08:00
|
|
|
|
} else {
|
2019-02-27 23:05:45 +08:00
|
|
|
|
logBuilder.append("===> {}: {} Parameters: {}\n");
|
|
|
|
|
logArgs.add(requestMethod);
|
|
|
|
|
logArgs.add(requestURI);
|
|
|
|
|
logArgs.add(JsonUtil.toJson(paraMap));
|
2018-12-24 11:58:45 +08:00
|
|
|
|
}
|
|
|
|
|
// 打印请求头
|
|
|
|
|
Enumeration<String> headers = request.getHeaderNames();
|
|
|
|
|
while (headers.hasMoreElements()) {
|
|
|
|
|
String headerName = headers.nextElement();
|
|
|
|
|
String headerValue = request.getHeader(headerName);
|
2019-02-27 23:05:45 +08:00
|
|
|
|
logBuilder.append("===headers=== {} : {}\n");
|
|
|
|
|
logArgs.add(headerName);
|
|
|
|
|
logArgs.add(headerValue);
|
2018-12-24 11:58:45 +08:00
|
|
|
|
}
|
|
|
|
|
// 打印执行时间
|
|
|
|
|
long startNs = System.nanoTime();
|
|
|
|
|
try {
|
|
|
|
|
Object result = point.proceed();
|
2019-02-27 23:05:45 +08:00
|
|
|
|
logBuilder.append("===Result=== {}\n");
|
|
|
|
|
logArgs.add(JsonUtil.toJson(result));
|
2018-12-24 11:58:45 +08:00
|
|
|
|
return result;
|
|
|
|
|
} finally {
|
|
|
|
|
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
|
2019-02-27 23:05:45 +08:00
|
|
|
|
logBuilder.append("<=== {}: {} ({} ms)");
|
|
|
|
|
logArgs.add(requestMethod);
|
|
|
|
|
logArgs.add(requestURI);
|
|
|
|
|
logArgs.add(tookMs);
|
|
|
|
|
logBuilder.append("\n================ Request End ================\n");
|
|
|
|
|
log.info(logBuilder.toString(), logArgs.toArray());
|
2018-12-24 11:58:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|