Commit 4fb19acc authored by yanzg's avatar yanzg

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

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