Commit 805755e1 authored by yanzg's avatar yanzg

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

parent 624d3779
......@@ -11,62 +11,57 @@ import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "日志对象")
public class LogVo extends BaseVo {
private static final long serialVersionUID = -8629960247077620458L;
/**
* 日志id
*/
@ApiModelProperty(value = "日志Id", required = true, example = "{{$uuid}}")
private volatile String logId;
private String logId;
/**
* 工程实例名
*/
@ApiModelProperty(value = "项目名称", required = true, example = "MQ|WEB|FEIGN")
private volatile String actionKey;
private String actionKey;
/**
* 请求接口地址
*/
@ApiModelProperty(value = "接口地址", required = true, example = "tbd-order/order/save")
private volatile String actionSubKey;
private String actionSubKey;
/**
* 请求内容
*/
@ApiModelProperty(value = "请求内容", required = true, example = "{}")
private volatile String content;
private String content;
/**
* 执行后的请求内容
*/
@ApiModelProperty(value = "执行后的请求内容,仅仅在内容有变化时写入", required = true, example = "{}")
private volatile String contentTo;
private String contentTo;
/**
* 返回参数
*/
@ApiModelProperty(value = "返回结果", required = true, example = "{}")
private volatile String result;
private String result;
/**
* 接口处理状态,是否有异常
*/
@ApiModelProperty(value = "执行状态", notes = "0表示成功,其他状态表示失败", required = true, example = "0")
private volatile String status;
private String status;
/**
* 使用时间
*/
@ApiModelProperty(value = "执行耗时", notes = "单位(毫秒)", required = true, example = "20")
private volatile int useTime;
private int useTime;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间", notes = "单位(毫秒)", required = false, example = "1987-11-24 23:15:18")
private volatile String createDate;
private String createDate;
/**
* 开始时间
*/
@ApiModelProperty(value = "开始时间", notes = "单位(毫秒)")
private long start = System.currentTimeMillis();
public String getLogId() {
return logId;
......@@ -140,4 +135,11 @@ public class LogVo extends BaseVo {
this.createDate = createDate;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
}
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.aop.log.AspectLogResult;
import com.yanzuoguang.cloud.aop.log.AspectLogStart;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.LogVo;
......@@ -52,12 +54,9 @@ public class AspectFeign {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogVo log = new LogVo();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
boolean clear = aspectLogStart.requestLogInit();
LogVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
Object result = null;
Exception ex = null;
......@@ -77,7 +76,7 @@ public class AspectFeign {
ex = e;
throw e;
} finally {
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, requestBody, result, ex, log);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.aop.log.AspectLogResult;
import com.yanzuoguang.cloud.aop.log.AspectLogStart;
import com.yanzuoguang.mq.service.MessageLogService;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.LogVo;
......@@ -11,10 +13,9 @@ import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
/**
* LogsAspect(接口请求日志切面)
* MQ处理切面
*
* @author: Kang
* @time: 2018年04月25日 11:43
* @author 颜佐光
*/
@Aspect
@Component
......@@ -61,11 +62,9 @@ public class AspectMq {
String url = aspectLogUrl.getMessageUrl(joinPoint, message);
String requestBody = aspectLogBody.getRequestBody(message);
LogVo log = new LogVo();
boolean clear = true;
aspectLogStart.requestLogInit();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
LogVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
Object result = null;
Exception ex = null;
try {
......@@ -78,7 +77,7 @@ public class AspectMq {
throw e;
} finally {
logService.logCurrentRemove();
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, requestBody, result, ex, log);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.AspectLogResult;
import com.yanzuoguang.cloud.aop.log.AspectLogStart;
import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -22,10 +24,9 @@ import java.lang.reflect.Type;
import java.util.Optional;
/**
* LogsAspect(接口请求日志切面)
* 接口请求日志切面
*
* @author: Kang
* @time: 2018年04月25日 11:43
* @author 颜佐光
*/
@Aspect
@Component
......@@ -74,7 +75,7 @@ public class AspectWeb {
*/
@Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class declaringType = joinPoint.getSignature().getDeclaringType();
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
// 判断是否网关
boolean isGateWay = cloudConfig.isGateWay();
......@@ -89,9 +90,7 @@ public class AspectWeb {
}
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogVo log = new LogVo();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
LogVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
Exception ex = null;
boolean isInit = false;
......@@ -103,7 +102,6 @@ public class AspectWeb {
for (Object arg : joinPoint.getArgs()) {
webAspectInit.init(arg);
}
// Log.info(declaringType, "初始化登陆");
result = executeMethod(joinPoint);
return result;
} catch (Exception e) {
......@@ -125,30 +123,27 @@ public class AspectWeb {
if (isInit) {
TokenHelper.remove();
}
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
aspectLogResult.responseLog(declaringType, TAG, url, clear, requestBody, result, ex, log);
}
}
/**
* 获取返回的类型
* 获取返回的类型
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
* @param joinPoint 执行方法
* @return 类型
*/
private Type getReturnType(ProceedingJoinPoint joinPoint) {
Method m = getMethod(joinPoint);
Type t = m.getAnnotatedReturnType().getType();
return t;
return m.getAnnotatedReturnType().getType();
}
/**
* 获取返回的至类型
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
* @param joinPoint 执行方法
* @return 方法
*/
private Method getMethod(ProceedingJoinPoint joinPoint) {
//获取返回值类型
......@@ -163,7 +158,7 @@ public class AspectWeb {
*
* @param joinPoint 需要执行的方法
* @return 返回结果
* @throws Throwable
* @throws Throwable 异常信息
*/
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
......
package com.yanzuoguang.cloud.aop;
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.cloud.aop.AspectLogBody;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
......@@ -41,17 +41,15 @@ public class AspectLogResult {
* @param tag 标签名称
* @param url 地址
* @param logFlag 请求日志
* @param start 开始时间
* @param requestBody 请求内容
* @param result 结果
* @param resultEx 异常结果
* @param log 内容
*/
// @Async
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
Object requestBody, Object result, Exception resultEx, LogVo log) {
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, Object requestBody, Object result, Exception resultEx, LogVo log) {
// 执行时间
long time = System.currentTimeMillis() - start;
long time = System.currentTimeMillis() - log.getStart();
// 全路径
String fullUrl = String.format("%s:%s", tag, url);
......
package com.yanzuoguang.cloud.aop;
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.cloud.aop.AspectLogBody;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.log.Log;
......@@ -31,15 +31,14 @@ public class AspectLogStart {
/**
* 记录请求日志
*
* @param tag
* @param url
* @param requestBody
* @param log
* @param logFlag
* @return
* @param tag 标记
* @param url 地址
* @param requestBody 请求内容
* @param logFlag 是否记录日志
*/
// @Async
public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) {
public LogVo requestLog(Class<?> cls, String tag, String url, Object requestBody, boolean logFlag) {
LogVo log = new LogVo();
try {
String fullUrl = String.format("%s:%s", tag, url);
logCountTime.start(fullUrl);
......@@ -55,6 +54,7 @@ public class AspectLogStart {
} catch (Exception ex) {
ExceptionHelper.PrintError(AspectLogStart.class, ex);
}
return log;
}
/**
......@@ -62,13 +62,11 @@ public class AspectLogStart {
*
* @return 返回日志是否清空
*/
protected boolean requestLogInit() {
public boolean requestLogInit() {
boolean clear = Log.threadCurrent() == null;
if (clear) {
Log.threadBegin();
}
return clear;
}
}
......@@ -3,7 +3,6 @@ package com.yanzuoguang.log;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
......@@ -32,7 +31,7 @@ public class LogCountTime {
/**
* 获取日志结果
*
* @return
* @return 今日日志结果
*/
public LogCountResult getTodayResult() {
return new LogCountResult(todayTime, todayMemoryCache.getValues());
......@@ -55,28 +54,32 @@ public class LogCountTime {
/**
* 获取缓存对象
*
* @param url 地址
* @param urlFrom 地址
* @return 缓存对象
*/
private LogUrlCountVo getCount(String url) {
private LogUrlCountVo getCount(String urlFrom) {
// 初始化日期
initToday();
if (url.contains(URL_PARA_START)) {
url = url.substring(0, url.indexOf(URL_PARA_START));
String key;
if (urlFrom.contains(URL_PARA_START)) {
key = urlFrom.substring(0, urlFrom.indexOf(URL_PARA_START));
}
else{
key = urlFrom;
}
// 缓存中获取对象
LogUrlCountVo ret = todayMemoryCache.get(url);
LogUrlCountVo ret = todayMemoryCache.get(key);
if (ret != null) {
return ret;
}
// 缓存中不存在则创建对象
synchronized (todayMemoryCache) {
ret = todayMemoryCache.get(url);
ret = todayMemoryCache.get(key);
if (ret != null) {
return ret;
}
ret = new LogUrlCountVo(url);
todayMemoryCache.put(url, ret);
ret = new LogUrlCountVo(key);
todayMemoryCache.put(key, 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