Commit 0773f52e authored by yanzg's avatar yanzg

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

parent c2a783f6
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLogResult;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -25,15 +24,14 @@ public class AspectFeign {
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
private final AspectLog aspectLog;
public AspectFeign(AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
public AspectFeign(AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
this.aspectLog = aspectLog;
}
/**
......@@ -55,8 +53,8 @@ public class AspectFeign {
String url = aspectLogUrl.getWebMethodUrl(joinPoint);
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Object result = null;
Exception ex = null;
......@@ -76,7 +74,7 @@ public class AspectFeign {
ex = e;
throw e;
} finally {
aspectLogResult.responseLog(log, result, ex);
aspectLog.result(log, result, ex);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.log.AspectLogResult;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.mq.service.MessageLogService;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -26,15 +25,16 @@ public class AspectMq {
private final MessageLogService logService;
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
private final AspectLog aspectLog;
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.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
this.aspectLog = aspectLog;
}
/**
......@@ -63,8 +63,8 @@ public class AspectMq {
String requestBody = aspectLogBody.getRequestBody(message);
boolean clear = true;
aspectLogStart.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Object result = null;
Exception ex = null;
try {
......@@ -77,7 +77,7 @@ public class AspectMq {
throw e;
} finally {
logService.logCurrentRemove();
aspectLogResult.responseLog(log, result, ex);
aspectLog.result(log, result, ex);
}
}
......
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.service.TokenServiceCall;
import com.yanzuoguang.log.AspectLogResult;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.token.TokenHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;
......@@ -39,18 +38,19 @@ public class AspectWeb {
private final CloudConfig cloudConfig;
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
private final AspectLog aspectLog;
public AspectWeb(TokenServiceCall tokenServiceCall, Optional<WebAspectInit> webAspectInit, CloudConfig cloudConfig,
AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody,
AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
public AspectWeb(TokenServiceCall tokenServiceCall,
Optional<WebAspectInit> webAspectInit,
CloudConfig cloudConfig,
AspectLogUrl aspectLogUrl,
AspectLogBody aspectLogBody,
AspectLog aspectLog) {
this.tokenServiceCall = tokenServiceCall;
this.cloudConfig = cloudConfig;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
this.aspectLog = aspectLog;
if (webAspectInit.isPresent()) {
this.webAspectInit = webAspectInit.get();
} else {
......@@ -89,8 +89,8 @@ public class AspectWeb {
}
}
Object requestBody = aspectLogBody.getRequestBody(joinPoint);
boolean clear = aspectLogStart.requestLogInit();
LogInfoVo log = aspectLogStart.requestLog(declaringType, TAG, url, requestBody, clear);
boolean clear = aspectLog.requestLogInit();
LogInfoVo log = aspectLog.start(declaringType, TAG, url, requestBody, clear);
Exception ex = null;
boolean isInit = false;
......@@ -123,7 +123,7 @@ public class AspectWeb {
if (isInit) {
TokenHelper.remove();
}
aspectLogResult.responseLog(log, result, ex);
aspectLog.result(log, result, ex);
}
}
......
......@@ -2,8 +2,7 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.log.AspectLogResult;
import com.yanzuoguang.log.AspectLogStart;
import com.yanzuoguang.log.AspectLog;
import com.yanzuoguang.log.LogInfoVo;
import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.Ref;
......@@ -25,19 +24,16 @@ public class DbExecuteImpl implements DbExecute {
private final JdbcTemplate jdbcTemplate;
private final DbExecutePrintSql printSql;
private final ConfigDb configDb;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
private final AspectLog aspectLog;
public DbExecuteImpl(JdbcTemplate jdbcTemplate,
DbExecutePrintSql printSql,
ConfigDb configDb,
AspectLogStart aspectLogStart,
AspectLogResult aspectLogResult) {
AspectLog aspectLog) {
this.jdbcTemplate = jdbcTemplate;
this.printSql = printSql;
this.configDb = configDb;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
this.aspectLog = aspectLog;
}
public JdbcTemplate getJdbc() {
......@@ -157,7 +153,7 @@ public class DbExecuteImpl implements DbExecute {
// 开始记录日志
String url = String.format("%s:%s", sqlInfo.getTargetClass().getSimpleName(), sqlInfo.getSqlName());
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();
Ref<Integer> row = new Ref<>(0);
......@@ -166,10 +162,10 @@ public class DbExecuteImpl implements DbExecute {
// 执行得函数
sqlFunction.accept(row, start);
// 写入日志
aspectLogResult.responseLog(log, row, null);
aspectLog.result(log, row, null);
} catch (Exception ex) {
// 写入日志
aspectLogResult.responseLog(log, row, ex);
aspectLog.result(log, row, ex);
throw ex;
} finally {
// 打印sql语句
......
package com.yanzuoguang.log;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
......@@ -8,27 +9,79 @@ import com.yanzuoguang.util.vo.CloudConfig;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 写入日志
*
* @author 颜佐光
*/
@Component
public class AspectLogResult {
public class AspectLog {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final LogString logString;
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.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 是否打印日志
* @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 {
* @param resultEx 异常结果
* @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())) {
return;
}
......@@ -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());
}
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e);
ExceptionHelper.PrintError(AspectLog.class, e);
} finally {
// 输出请求结果
logLocal.result(log);
......@@ -90,7 +143,7 @@ public class AspectLogResult {
}
return responseResult;
} catch (Exception e) {
ExceptionHelper.PrintError(AspectLogResult.class, e);
ExceptionHelper.PrintError(AspectLog.class, 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