Commit 0773f52e authored by yanzg's avatar yanzg

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

parent c2a783f6
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLogResult; import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.LogInfoVo; import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
...@@ -25,15 +24,14 @@ public class AspectFeign { ...@@ -25,15 +24,14 @@ public class AspectFeign {
private final AspectLogUrl aspectLogUrl; private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart; private final AspectLog aspectLog;
private final AspectLogResult aspectLogResult;
public AspectFeign(AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody, public AspectFeign(AspectLogUrl aspectLogUrl,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) { AspectLogBody aspectLogBody,
AspectLog aspectLog) {
this.aspectLogUrl = aspectLogUrl; this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody; this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart; this.aspectLog = aspectLog;
this.aspectLogResult = aspectLogResult;
} }
/** /**
...@@ -55,8 +53,8 @@ public class AspectFeign { ...@@ -55,8 +53,8 @@ public class AspectFeign {
String url = aspectLogUrl.getWebMethodUrl(joinPoint); String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint); Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit(); boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear); LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Object result = null; Object result = null;
Exception ex = null; Exception ex = null;
...@@ -76,7 +74,7 @@ public class AspectFeign { ...@@ -76,7 +74,7 @@ public class AspectFeign {
ex = e; ex = e;
throw e; throw e;
} finally { } finally {
aspectLogResult.responseLog(log, result, ex); aspectLog.result(log, result, ex);
} }
} }
......
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLogResult; import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.LogInfoVo; import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.mq.service.MessageLogService; import com.yanzuoguang.mq.service.MessageLogService;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
...@@ -26,15 +25,16 @@ public class AspectMq { ...@@ -26,15 +25,16 @@ public class AspectMq {
private final MessageLogService logService; private final MessageLogService logService;
private final AspectLogUrl aspectLogUrl; private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart; private final AspectLog aspectLog;
private final AspectLogResult aspectLogResult;
public AspectMq(MessageLogService logService, AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody, AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) { public AspectMq(MessageLogService logService,
AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
this.logService = logService; this.logService = logService;
this.aspectLogUrl = aspectLogUrl; this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody; this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart; this.aspectLog = aspectLog;
this.aspectLogResult = aspectLogResult;
} }
/** /**
...@@ -63,8 +63,8 @@ public class AspectMq { ...@@ -63,8 +63,8 @@ public class AspectMq {
String requestBody = aspectLogBody.getRequestBody(message); String requestBody = aspectLogBody.getRequestBody(message);
boolean clear = true; boolean clear = true;
aspectLogStart.requestLogInit(); aspectLog.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear); LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Object result = null; Object result = null;
Exception ex = null; Exception ex = null;
try { try {
...@@ -77,7 +77,7 @@ public class AspectMq { ...@@ -77,7 +77,7 @@ public class AspectMq {
throw e; throw e;
} finally { } finally {
logService.logCurrentRemove(); logService.logCurrentRemove();
aspectLogResult.responseLog(log, result, ex); aspectLog.result(log, result, ex);
} }
} }
......
package com.yanzuoguang.cloud.aop; package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.service.TokenServiceCall; import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.log.AspectLogResult; import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.LogInfoVo; import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.token.TokenHelper; import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
...@@ -39,18 +38,19 @@ public class AspectWeb { ...@@ -39,18 +38,19 @@ public class AspectWeb {
private final CloudConfig cloudConfig; private final CloudConfig cloudConfig;
private final AspectLogUrl aspectLogUrl; private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody; private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart; private final AspectLog aspectLog;
private final AspectLogResult aspectLogResult;
public AspectWeb(TokenServiceCall tokenServiceCall, Optional<WebAspectInit> webAspectInit, CloudConfig cloudConfig, public AspectWeb(TokenServiceCall tokenServiceCall,
AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody, Optional<WebAspectInit> webAspectInit,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) { CloudConfig cloudConfig,
AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
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.aspectLogStart = aspectLogStart; this.aspectLog = aspectLog;
this.aspectLogResult = aspectLogResult;
if (webAspectInit.isPresent()) { if (webAspectInit.isPresent()) {
this.webAspectInit = webAspectInit.get(); this.webAspectInit = webAspectInit.get();
} else { } else {
...@@ -89,8 +89,8 @@ public class AspectWeb { ...@@ -89,8 +89,8 @@ public class AspectWeb {
} }
} }
Object requestBody = aspectLogBody.getRequestBody(joinPoint); Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit(); boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear); LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Exception ex = null; Exception ex = null;
boolean isInit = false; boolean isInit = false;
...@@ -123,7 +123,7 @@ public class AspectWeb { ...@@ -123,7 +123,7 @@ public class AspectWeb {
if (isInit) { if (isInit) {
TokenHelper.remove(); TokenHelper.remove();
} }
aspectLogResult.responseLog(log, result, ex); aspectLog.result(log, result, ex);
} }
} }
......
...@@ -2,8 +2,7 @@ package com.yanzuoguang.db.impl; ...@@ -2,8 +2,7 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb; import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.db.DbExecute; import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.log.AspectLogResult; import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.LogInfoVo; import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.util.vo.MapRow; import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.Ref; import com.yanzuoguang.util.vo.Ref;
...@@ -25,19 +24,16 @@ public class DbExecuteImpl implements DbExecute { ...@@ -25,19 +24,16 @@ public class DbExecuteImpl implements DbExecute {
private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
private final DbExecutePrintSql printSql; private final DbExecutePrintSql printSql;
private final ConfigDb configDb; private final ConfigDb configDb;
private final AspectLogStart aspectLogStart; private final AspectLog aspectLog;
private final AspectLogResult aspectLogResult;
public DbExecuteImpl(JdbcTemplate jdbcTemplate, public DbExecuteImpl(JdbcTemplate jdbcTemplate,
DbExecutePrintSql printSql, DbExecutePrintSql printSql,
ConfigDb configDb, ConfigDb configDb,
AspectLogStart aspectLogStart, AspectLog aspectLog) {
AspectLogResult aspectLogResult) {
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
this.printSql = printSql; this.printSql = printSql;
this.configDb = configDb; this.configDb = configDb;
this.aspectLogStart = aspectLogStart; this.aspectLog = aspectLog;
this.aspectLogResult = aspectLogResult;
} }
public JdbcTemplate getJdbc() { public JdbcTemplate getJdbc() {
...@@ -157,7 +153,7 @@ public class DbExecuteImpl implements DbExecute { ...@@ -157,7 +153,7 @@ public class DbExecuteImpl implements DbExecute {
// 开始记录日志 // 开始记录日志
String url = String.format("%s:%s", sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName()); String url = String.format("%s:%s", sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName());
String sqlText = DbPrintSqlDefault.getStringSql(sqlInfo.getSql(), sqlInfo.getParas()); String sqlText = DbPrintSqlDefault.getStringSql(sqlInfo.getSql(), sqlInfo.getParas());
LogInfoVo log = aspectLogStart.requestLog(sqlInfo.getTargetClass(), "Sql", url, sqlText, true); LogInfoVo log = aspectLog.start(sqlInfo.getTargetClass(), "Sql", url, sqlText, true);
// 开始时间 // 开始时间
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Ref<Integer> row = new Ref<>(0); Ref<Integer> row = new Ref<>(0);
...@@ -166,10 +162,10 @@ public class DbExecuteImpl implements DbExecute { ...@@ -166,10 +162,10 @@ public class DbExecuteImpl implements DbExecute {
// 执行得函数 // 执行得函数
sqlFunction.accept(row, start); sqlFunction.accept(row, start);
// 写入日志 // 写入日志
aspectLogResult.responseLog(log, row, null); aspectLog.result(log, row, null);
} catch (Exception ex) { } catch (Exception ex) {
// 写入日志 // 写入日志
aspectLogResult.responseLog(log, row, ex); aspectLog.result(log, row, ex);
throw ex; throw ex;
} finally { } finally {
// 打印sql语句 // 打印sql语句
......
package com.yanzuoguang.log; package com.yanzuoguang.log;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
...@@ -8,27 +9,79 @@ import com.yanzuoguang.util.vo.CloudConfig; ...@@ -8,27 +9,79 @@ import com.yanzuoguang.util.vo.CloudConfig;
import com.yanzuoguang.util.vo.ResponseResult; import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date;
/** /**
* 写入日志 * 写入日志
* *
* @author 颜佐光 * @author 颜佐光
*/ */
@Component @Component
public class AspectLogResult { public class AspectLog {
private final CloudConfig cloudConfig; private final CloudConfig cloudConfig;
private final LogLocal logLocal; private final LogLocal logLocal;
private final LogString logString; private final LogString logString;
private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功"); private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功");
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, LogString logString) { public AspectLog(CloudConfig cloudConfig, LogLocal logLocal, LogString logString) {
this.cloudConfig = cloudConfig; this.cloudConfig = cloudConfig;
this.logLocal = logLocal; this.logLocal = logLocal;
this.logString = logString; this.logString = logString;
} }
/**
* 获取是否清空日志的标签
*
* @return 返回日志是否清空
*/
public boolean requestLogInit() {
boolean clear = Log.threadCurrent() == null;
if (clear) {
Log.threadBegin();
}
return clear;
}
/**
* 记录请求日志
*
* @param tag 标记
* @param url 地址
* @param requestBody 请求内容
* @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;
}
// 声明日志对象
LogInfoVo log = new LogInfoVo();
log.setCls(cls);
log.setLogId(StringHelper.getNewID());
log.setCreateDate(DateHelper.getDateTimeString(new Date()));
// 写入其他对象
log.setApplicationName(this.cloudConfig.getApplicationName());
log.setTag(tag);
log.setUrl(url);
log.setContent(body);
log.setLogFlag(logFlag);
// 开始记录到日志对象中,用于监测系统执行情况,超时时,则直接打印出日志
this.logLocal.startLog(log);
// 输出日志
if (log.isLogFlag() && this.cloudConfig.isLogCommon()) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, body);
}
return log;
}
/** /**
* 保存日志 * 保存日志
* *
...@@ -36,7 +89,7 @@ public class AspectLogResult { ...@@ -36,7 +89,7 @@ public class AspectLogResult {
* @param resultEx 异常结果 * @param resultEx 异常结果
* @param log 内容 * @param log 内容
*/ */
public void responseLog(LogInfoVo log, Object resultFrom, Exception resultEx) { public void result(LogInfoVo log, Object resultFrom, Exception resultEx) {
if (log == null || StringHelper.isEmpty(log.getLogId())) { if (log == null || StringHelper.isEmpty(log.getLogId())) {
return; return;
} }
...@@ -64,7 +117,7 @@ public class AspectLogResult { ...@@ -64,7 +117,7 @@ public class AspectLogResult {
Log.error(log.getCls(), resultEx, "%s [ %s ] time %d ms, result: %s", log.getTag(), log.getUrl(), log.getUseTime(), log.getResult()); Log.error(log.getCls(), resultEx, "%s [ %s ] time %d ms, result: %s", log.getTag(), log.getUrl(), log.getUseTime(), log.getResult());
} }
} catch (Exception e) { } catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e); ExceptionHelper.PrintError(AspectLog.class, e);
} finally { } finally {
// 输出请求结果 // 输出请求结果
logLocal.result(log); logLocal.result(log);
...@@ -90,7 +143,7 @@ public class AspectLogResult { ...@@ -90,7 +143,7 @@ public class AspectLogResult {
} }
return responseResult; return responseResult;
} catch (Exception e) { } catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e); ExceptionHelper.PrintError(AspectLog.class, e);
return ExceptionHelper.getError(e); return ExceptionHelper.getError(e);
} }
} }
......
package com.yanzuoguang.log;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.CloudConfig;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 写入日志
*
* @author 颜佐光
*/
@Component
public class AspectLogStart {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final LogString logString;
public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, LogString logString) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.logString = logString;
}
/**
* 获取是否清空日志的标签
*
* @return 返回日志是否清空
*/
public boolean requestLogInit() {
boolean clear = Log.threadCurrent() == null;
if (clear) {
Log.threadBegin();
}
return clear;
}
/**
* 记录请求日志
*
* @param tag 标记
* @param url 地址
* @param requestBody 请求内容
* @param logFlag 是否记录日志
*/
public LogInfoVo requestLog(Class<?> cls, String tag, String url, Object requestBody, boolean logFlag) {
String body;
try {
body = logString.getBodyString(requestBody);
} catch (Exception ex) {
ExceptionHelper.PrintError(AspectLogStart.class, ex);
body = StringHelper.EMPTY;
}
// 声明日志对象
LogInfoVo log = new LogInfoVo();
log.setCls(cls);
log.setLogId(StringHelper.getNewID());
log.setCreateDate(DateHelper.getDateTimeString(new Date()));
// 写入其他对象
log.setApplicationName(this.cloudConfig.getApplicationName());
log.setTag(tag);
log.setUrl(url);
log.setContent(body);
log.setLogFlag(logFlag);
// 开始记录到日志对象中,用于监测系统执行情况,超时时,则直接打印出日志
this.logLocal.startLog(log);
// 输出日志
if (log.isLogFlag() && this.cloudConfig.isLogCommon()) {
Log.info(cls, " %s [ %s ] request: %s", tag, url, body);
}
return 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