Commit 4fb19acc authored by yanzg's avatar yanzg

修复异常提醒,从而正确的跟踪异常信息

parent 6ed0ffd1
......@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.log.LogString;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.ResponseResult;
......@@ -25,13 +26,16 @@ public class AspectFeign {
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLog aspectLog;
private final LogString logString;
public AspectFeign(AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
AspectLog aspectLog,
LogString logString) {
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLog = aspectLog;
this.logString = logString;
}
/**
......@@ -50,11 +54,12 @@ public class AspectFeign {
@Around(value = "feignAspect()")
public Object requestFeignAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
AspectLogUrl.WebUrlInfo webMethodUrl = aspectLogUrl.getWebMethodBaseUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
String bodyString = String.format("地址:%s 内容:%s", webMethodUrl.getRequestUrl(), logString.getBodyString(requestBody));
LogInfoVo log = aspectLog.start(declaringType, TAG, webMethodUrl.getConfigUrl(), bodyString, clear);
Object result = null;
Exception ex = null;
......
......@@ -26,89 +26,137 @@ import java.util.List;
*/
@Component
public class AspectLogUrl {
/**
* /**
* 获取方法名称
*
* @return 方法路径
*/
public String getWebMethodUrl(ProceedingJoinPoint joinPoint) {
String url = StringHelper.EMPTY;
public WebUrlInfo getWebMethodBaseUrl(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
String name = signature.getName();
FeignClient feignClient = declaringType.getAnnotation(FeignClient.class);
// 类路径
RequestMapping[] classRequests = declaringType.getAnnotationsByType(RequestMapping.class);
PostMapping[] classPosts = declaringType.getAnnotationsByType(PostMapping.class);
GetMapping[] classGets = declaringType.getAnnotationsByType(GetMapping.class);
WebUrlInfo url = getWebMethodUrlHandle(signature, declaringType);
String baseUrl = String.format("%s:%s", declaringType.getSimpleName(), name);
if (url == null) {
return new WebUrlInfo(baseUrl, baseUrl);
}
return new WebUrlInfo(String.format("%s:%s", baseUrl, url.configUrl), String.format("%s:%s", baseUrl, url.requestUrl));
}
List<Annotation> annotation = new ArrayList<>();
ArrayHelper.addList(annotation, classRequests, classPosts, classGets);
/**
* 获取方法名称
*
* @return 方法路径
*/
public WebUrlInfo getWebMethodUrl(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
return getWebMethodUrlHandle(signature, declaringType);
}
String baseUrl = String.format("%s:%s", declaringType.getSimpleName(), name);
if (!annotation.isEmpty() && signature instanceof MethodSignature) {
/**
* 获取地址最终处理方法
*
* @param signature 方法签名
* @param declaringType 类型
* @return 返回地址
*/
private WebUrlInfo getWebMethodUrlHandle(Signature signature, Class<?> declaringType) {
FeignClient feignClient = declaringType.getAnnotation(FeignClient.class);
// 类路径
List<Annotation> classAnnotation = getClassRequestAnnotations(declaringType);
if (!classAnnotation.isEmpty() && signature instanceof MethodSignature) {
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod();
// 方法路径
RequestMapping[] requests = targetMethod.getAnnotationsByType(RequestMapping.class);
PostMapping[] posts = targetMethod.getAnnotationsByType(PostMapping.class);
GetMapping[] gets = targetMethod.getAnnotationsByType(GetMapping.class);
url = getFeignOrRequestUrl(feignClient, classRequests, classPosts, classGets, requests, posts, gets);
}
if (StringHelper.isEmpty(url)) {
return baseUrl;
List<Annotation> methodAnnotation = getMethodRequestAnnotations(targetMethod);
// 请求地址
return getFeignOrRequestUrl(feignClient, classAnnotation, methodAnnotation);
}
return String.format("%s:%s", baseUrl, url);
return null;
}
/**
* 获取feign请求地址
*
* @param feignClient
* @param classRequests
* @param classPosts
* @param classGets
* @param requests
* @param posts
* @param gets
* @return
* @param feignClient 客户端
* @param classAnnotation 类地址
* @param methodAnnotation 方法地址
* @return 最终返回地址
*/
private String getFeignOrRequestUrl(FeignClient feignClient, RequestMapping[] classRequests, PostMapping[] classPosts, GetMapping[] classGets, RequestMapping[] requests, PostMapping[] posts, GetMapping[] gets) {
StringBuilder sb = new StringBuilder();
private WebUrlInfo getFeignOrRequestUrl(FeignClient feignClient, List<Annotation> classAnnotation, List<Annotation> methodAnnotation) {
// 配置地址
StringBuilder sbConfig = new StringBuilder();
// HTTP请求地址
StringBuilder sbRequest = new StringBuilder();
if (feignClient != null) {
sb.append(feignClient.value());
sb.append(":");
sbConfig.append(feignClient.value());
sbConfig.append(":");
} else {
HttpServletRequest request = CookiesHelper.getRequest();
sbRequest.append(request.getRequestURI());
if (!StringHelper.isEmpty(request.getQueryString())) {
sbRequest.append("?");
sbRequest.append(request.getQueryString());
}
}
appendUrl(sbConfig, classAnnotation);
appendUrl(sbConfig, methodAnnotation);
return new WebUrlInfo(sbConfig.toString(), sbRequest.toString());
}
List<RequestMapping> reqList = ArrayHelper.mergeList(classRequests, requests);
List<PostMapping> postList = ArrayHelper.mergeList(classPosts, posts);
List<GetMapping> getList = ArrayHelper.mergeList(classGets, gets);
for (RequestMapping item : reqList) {
private List<Annotation> getClassRequestAnnotations(Class<?> clazz) {
List<Annotation> annotation = new ArrayList<>();
ArrayHelper.addList(annotation,
clazz.getAnnotationsByType(RequestMapping.class),
clazz.getAnnotationsByType(PostMapping.class),
clazz.getAnnotationsByType(GetMapping.class)
);
return annotation;
}
private List<Annotation> getMethodRequestAnnotations(Method method) {
List<Annotation> annotation = new ArrayList<>();
ArrayHelper.addList(annotation,
method.getAnnotationsByType(RequestMapping.class),
method.getAnnotationsByType(PostMapping.class),
method.getAnnotationsByType(GetMapping.class)
);
return annotation;
}
private void appendUrl(StringBuilder sb, List<Annotation> annotations) {
for (Annotation annotation : annotations) {
String url = StringHelper.EMPTY;
if (annotation instanceof RequestMapping) {
RequestMapping item = (RequestMapping) annotation;
if (item.value().length > 0) {
sb.append(item.value()[0]);
url = item.value()[0];
}
}
for (PostMapping item : postList) {
} else if (annotation instanceof PostMapping) {
PostMapping item = (PostMapping) annotation;
if (item.value().length > 0) {
sb.append(item.value()[0]);
url = item.value()[0];
}
}
for (GetMapping item : getList) {
} else if (annotation instanceof GetMapping) {
PostMapping item = (PostMapping) annotation;
if (item.value().length > 0) {
sb.append(item.value()[0]);
url = item.value()[0];
}
}
} else {
HttpServletRequest request = CookiesHelper.getRequest();
sb.append(request.getRequestURI());
if (!StringHelper.isEmpty(request.getQueryString())) {
sb.append("?");
sb.append(request.getQueryString());
// 地址处理,源地址最后增加斜杠
if (!StringHelper.compare(sb.substring(sb.length() - 1, sb.length()), "/")) {
sb.append("/");
}
// 地址处理,来源地址去掉最后的斜杠
url = StringHelper.trimStart(url, "/");
sb.append(url);
}
return sb.toString();
}
/**
......@@ -151,4 +199,31 @@ public class AspectLogUrl {
return String.format("%s:%s", baseUrl, url);
}
public static class WebUrlInfo {
/**
* 配置地址
*/
private final String configUrl;
/**
* 请求地址
*/
private final String requestUrl;
public WebUrlInfo(String configUrl, String requestUrl) {
this.configUrl = configUrl;
this.requestUrl = requestUrl;
}
public String getConfigUrl() {
return configUrl;
}
public String getRequestUrl() {
if (StringHelper.isEmpty(requestUrl)) {
return configUrl;
}
return requestUrl;
}
}
}
......@@ -3,6 +3,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.log.LogString;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
......@@ -40,18 +41,21 @@ public class AspectWeb {
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLog aspectLog;
private final LogString logString;
public AspectWeb(TokenServiceCall tokenServiceCall,
Optional<WebAspectInit> webAspectInit,
CloudConfig cloudConfig,
AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
AspectLog aspectLog,
LogString logString) {
this.tokenServiceCall = tokenServiceCall;
this.cloudConfig = cloudConfig;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLog = aspectLog;
this.logString = logString;
if (webAspectInit.isPresent()) {
this.webAspectInit = webAspectInit.get();
} else {
......@@ -77,7 +81,7 @@ public class AspectWeb {
@Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
AspectLogUrl.WebUrlInfo webMethodUrl = aspectLogUrl.getWebMethodBaseUrl(joinPoint);
// 判断是否网关
boolean isGateWay = cloudConfig.isGateWay();
if (isGateWay) {
......@@ -85,13 +89,15 @@ public class AspectWeb {
// 网关不进行任何拦截处理
return executeMethod(joinPoint);
} catch (Exception ex) {
System.err.println("请求地址错误:" + url);
System.err.println("请求地址错误:" + webMethodUrl.getRequestUrl());
throw ex;
}
}
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, UrlHelper.getPage(url), String.format("请求地址:%s 请求内容:%s", url, requestBody), clear);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
String bodyString = String.format("地址:%s 内容:%s", webMethodUrl.getRequestUrl(), logString.getBodyString(requestBody));
LogInfoVo log = aspectLog.start(declaringType, TAG, UrlHelper.getPage(webMethodUrl.getConfigUrl()), bodyString, clear);
Exception ex = null;
boolean isInit = false;
......@@ -164,4 +170,6 @@ public class AspectWeb {
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
}
......@@ -47,20 +47,13 @@ public class AspectLog {
/**
* 记录请求日志
*
* @param tag 标记
* @param url 地址
* @param requestBody 请求内容
* @param logFlag 是否打印日志
* @param tag 标记
* @param url 地址
* @param body 请求内容
* @param logFlag 是否打印日志
* @return 继续记录日志的对象
*/
public LogInfoVo start(Class<?> cls, String tag, String url, Object requestBody, boolean logFlag) {
String body;
try {
body = logString.getBodyString(requestBody);
} catch (Exception ex) {
ExceptionHelper.PrintError(AspectLog.class, ex);
body = StringHelper.EMPTY;
}
public LogInfoVo start(Class<?> cls, String tag, String url, String body, boolean logFlag) {
// 声明日志对象
LogInfoVo log = new LogInfoVo();
......
package com.yanzuoguang.log;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.CloudConfig;
import org.springframework.stereotype.Component;
......@@ -39,12 +41,16 @@ public class LogString {
*/
public String getBodyString(Object body) {
String ret;
if (body instanceof String) {
ret = getMaxString((String) body);
} else {
ret = getMaxString(JsonHelper.serialize(body));
try {
if (body instanceof String) {
ret = getMaxString((String) body);
} else {
ret = getMaxString(JsonHelper.serialize(body));
}
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLog.class, e);
ret = StringHelper.EMPTY;
}
return ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment