Commit 5e36394d authored by yanzg's avatar yanzg

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

parent 3124779c
......@@ -19,51 +19,62 @@ public class AspectLogResult {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final LogString logString;
private final LogCountTime logCountTime;
private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功");
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, LogString logString, LogCountTime logCountTime) {
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, LogString logString) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.logString = logString;
this.logCountTime = logCountTime;
}
/**
* 保存日志
*
* @param result 结果
* @param resultEx 异常结果
* @param log 内容
* @param resultFrom 结果
* @param resultEx 异常结果
* @param log 内容
*/
// @Async
public void responseLog(LogInfoVo log, Object result, Exception resultEx) {
// 执行时间
long time = System.currentTimeMillis() - log.getStart();
// 全路径
String fullUrl = String.format("%s:%s", tag, url);
public void responseLog(LogInfoVo log, Object resultFrom, Exception resultEx) {
if (log == null || StringHelper.isEmpty(log.getLogId())) {
return;
}
try {
ResponseResult<?> responseResult = getJson(resultFrom, resultEx);
String json = JsonHelper.serialize(responseResult);
String result = logString.getMaxString(json);
log.setResult(result);
log.setEnd(System.currentTimeMillis());
long useTime = log.getEnd() - log.getStart();
log.setUseTime((int) useTime);
if (resultEx != null) {
log.setStatus(LogInfoVo.STATUS_ERROR);
} else {
log.setStatus(LogInfoVo.STATUS_OK);
}
// 输出请求结果
logLocal.result(log);
// 输出日志标记
boolean isLogCommon = logFlag && this.cloudConfig.isLogCommon();
boolean isLogCommon = log.isLogFlag() && this.cloudConfig.isLogCommon();
boolean isError = resultEx != null;
boolean isLogDisplay = isLogCommon || isError;
// 记录请求时间
logCountTime.finish(fullUrl, time, isError);
// ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()))
if (StringHelper.isEmpty(log.getLogId())) {
return;
if (isLogDisplay) {
Log.error(log.getCls(), resultEx, "%s [ %s ] time %d ms, result: %s", log.getTag(), log.getUrl(), log.getUseTime(), log.getResult());
}
if (!isLogDisplay) {
logLocal.remove(log);
return;
}
String body = logString.getBodyString(requestBody);
if (!StringHelper.compare(log.getContent(), body)) {
log.setContentTo(body);
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e);
} finally {
logLocal.remove(log);
if (log.isLogFlag()) {
Log.threadCommit();
}
}
}
private ResponseResult<?> getJson(Object result, Exception resultEx) {
try {
// 处理结果
ResponseResult<?> responseResult;
if (result instanceof ResponseResult) {
......@@ -75,19 +86,10 @@ public class AspectLogResult {
} else {
responseResult = responseDefault;
}
String fullUrl = String.format("%s:%s", tag, url);
logCountTime.start(fullUrl);
String json = JsonHelper.serialize(responseResult);
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
logLocal.result(log, responseResult.getCode(), json, isError);
return responseResult;
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e);
} finally {
if (logFlag) {
Log.threadCommit();
}
return ExceptionHelper.getError(e);
}
}
}
......@@ -48,7 +48,6 @@ public class AspectLogStart {
* @param requestBody 请求内容
* @param logFlag 是否记录日志
*/
// @Async
public LogInfoVo requestLog(Class<?> cls, String tag, String url, Object requestBody, boolean logFlag) {
String body;
try {
......@@ -67,11 +66,11 @@ public class AspectLogStart {
log.setTag(tag);
log.setUrl(url);
log.setContent(body);
log.setLogFlag(logFlag && this.cloudConfig.isLogCommon());
log.setLogFlag(logFlag);
// 开始记录到日志对象中,用于监测系统执行情况,超时时,则直接打印出日志
this.logLocal.startLog(log);
// 输出日志
if (log.isLogFlag()) {
if (log.isLogFlag() && this.cloudConfig.isLogCommon()) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, body);
}
return log;
......
......@@ -15,5 +15,5 @@ public interface LogFeignBase {
* @param log 需要保存的日志对象
* @return 日志服务返回结果
*/
ResponseResult<String> save(LogVo log);
ResponseResult<String> save(LogInfoVo log);
}
......@@ -18,7 +18,7 @@ public class LogFeignDefault implements LogFeignBase {
* @return 日志服务返回结果
*/
@Override
public ResponseResult<String> save(LogVo log) {
public ResponseResult<String> save(LogInfoVo log) {
return ResponseResult.result("处理成功");
}
}
......@@ -4,56 +4,71 @@ import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModelProperty;
public class LogInfoVo extends BaseVo {
public static final int STATUS_WAIT = -1;
public static final int STATUS_ERROR = 0;
public static final int STATUS_OK = 1;
private static final long serialVersionUID = -8629960247077620458L;
/**
* 日志id
*/
@ApiModelProperty(value = "日志Id", required = true, example = "{{$uuid}}")
@ApiModelProperty(notes = "日志Id", required = true, example = "{{$uuid}}")
private String logId;
/**
* 来源类
*/
@ApiModelProperty(notes = "来源类")
private Class<?> cls;
/**
* 项目名称
*/
@ApiModelProperty(value = "项目名称", required = true)
@ApiModelProperty(notes = "项目名称", required = true)
private String applicationName;
/**
* 触发点
*/
@ApiModelProperty(value = "触发点", required = true, example = "MQ|WEB|FEIGN")
@ApiModelProperty(notes = "触发点", required = true, example = "MQ|WEB|FEIGN")
private String tag;
/**
* 请求接口地址
*/
@ApiModelProperty(value = "接口地址", required = true, example = "tbd-order/order/save")
@ApiModelProperty(notes = "接口地址", required = true, example = "tbd-order/order/save")
private String url;
/**
* 请求内容
*/
@ApiModelProperty(value = "请求内容", required = true, example = "{}")
@ApiModelProperty(notes = "请求内容", required = true, example = "{}")
private String content;
/**
* 返回参数
*/
@ApiModelProperty(value = "返回结果", required = true, example = "{}")
@ApiModelProperty(notes = "返回结果", required = true, example = "{}")
private String result;
/**
* 接口处理状态,是否有异常
*/
@ApiModelProperty(value = "执行状态", notes = "0表示成功,其他状态表示失败,-1表示执行中", required = true, example = "0")
private int status;
@ApiModelProperty(notes = "执行状态: -1表示执行中,0-表示失败,1表示成功", required = true, example = "0")
private int status = STATUS_WAIT;
/**
* 开始时间
*/
@ApiModelProperty(value = "开始时间", notes = "单位(毫秒)")
@ApiModelProperty(notes = "开始时间:单位(毫秒)")
private long start = System.currentTimeMillis();
/**
* 结束时间
*/
@ApiModelProperty(notes = "结束时间: 单位(毫秒)")
private long end = System.currentTimeMillis();
/**
* 使用时间
*/
@ApiModelProperty(value = "执行耗时", notes = "单位(毫秒)", required = true, example = "20")
@ApiModelProperty(notes = "执行耗时: 单位(毫秒)", required = true, example = "20")
private int useTime;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间", notes = "单位(毫秒)", required = false, example = "1987-11-24 23:15:18")
@ApiModelProperty(notes = "创建时间: 单位(毫秒)", example = "1987-11-24 23:15:18")
private String createDate;
/**
* 是否记录日志
......@@ -69,6 +84,14 @@ public class LogInfoVo extends BaseVo {
this.logId = logId;
}
public Class<?> getCls() {
return cls;
}
public void setCls(Class<?> cls) {
this.cls = cls;
}
public String getApplicationName() {
return applicationName;
}
......@@ -125,6 +148,14 @@ public class LogInfoVo extends BaseVo {
this.start = start;
}
public long getEnd() {
return end;
}
public void setEnd(long end) {
this.end = end;
}
public int getUseTime() {
return useTime;
}
......
......@@ -34,6 +34,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
private final LogBase logBase;
private final CloudConfig cloudConfig;
private final List<LogFilter> logFilters;
private final LogCountTime logCountTime;
/**
* 缓存队列
*/
......@@ -53,7 +54,7 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 开始记录日志
*
* @param log 日志对象
* @param log 日志对象
*/
public void startLog(LogInfoVo log) {
// 声明超时对象
......@@ -64,12 +65,9 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
/**
* 写入状态
*
* @param log 日志对象
* @param status 状态
* @param result 结果
* @param isError 是否出错
* @param log 日志对象
*/
public void result(LogInfoVo log, String status, String result, boolean isError) {
public void result(LogInfoVo log) {
if (log == null) {
return;
}
......@@ -78,19 +76,27 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
if (isLog) {
this.remove(log);
} else {
this.resultWrite(log, status, result, isError);
this.resultWrite(log);
}
String fullUrl = String.format("%s:%s", tag, url);
logCountTime.start(fullUrl);
// 全路径
String fullUrl = String.format("%s:%s", tag, url);
// 执行时间
long time = System.currentTimeMillis() - log.getStart();
// 记录请求时间
logCountTime.finish(fullUrl, time, isError);
}
/**
* 写入状态
*
* @param log 日志对象
* @param status 状态
* @param result 结果
* @param write 写入
*/
private void resultWrite(LogInfoVo log, String status, String result, boolean write) {
private void resultWrite(LogInfoVo log) {
if (log == null || StringHelper.isEmpty(log.getLogId())) {
return;
}
......@@ -102,12 +108,6 @@ public class LogLocal implements ThreadNext.Next, InitializingBean {
} else {
timeout = cache.remove(log.getLogId());
}
if (timeout != null) {
long useTime = System.currentTimeMillis() - timeout.getStart();
log.setUseTime((int) useTime);
}
log.setStatus(status);
log.setResult(result);
if (write) {
logBase.addLog(log);
......
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