Commit f451bbd9 authored by yanzg's avatar yanzg

常规BUG的修改

parent bdb5ce86
......@@ -107,23 +107,34 @@ public class Log {
/**
* 当前线程特殊处理异常,一旦调用必须调用 threadCommit 函数
*/
public static void threadBegin() {
public static LogDate threadBegin() {
// 获取当前线程编号
String threadId = getThreadId();
LogDate logDate = new LogDate();
logDate.clear();
threadCache.put(threadId, logDate);
return logDate;
}
// System.out.println(threadId + " threadBegin");
/**
* 当前县城日志对象
*
* @return
*/
public static LogDate threadCurrent() {
String threadId = getThreadId();
LogDate log = threadCache.containsKey(threadId) ? threadCache.get(threadId) : null;
return log;
}
/**
* 当前线程结束处理特殊异常
*/
public static void threadCommit() {
public static LogDate threadCommit() {
String threadId = getThreadId();
LogDate log = threadCurrent();
threadCache.remove(threadId);
// System.out.println(threadId + " threadCommit");
return log;
}
/**
......
......@@ -38,6 +38,11 @@ public class LogVo extends BaseVo {
*/
private int status;
/**
* 使用时间
*/
private int useTime;
/**
* 创建时间
*/
......@@ -91,6 +96,14 @@ public class LogVo extends BaseVo {
this.status = status;
}
public int getUseTime() {
return useTime;
}
public void setUseTime(int useTime) {
this.useTime = useTime;
}
public String getCreateDate() {
return createDate;
}
......
......@@ -37,14 +37,15 @@ import java.lang.reflect.Type;
@Component
public class WebAspect extends BaseRequestAspect {
private static final Logger logger = LoggerFactory.getLogger(BaseRequestAspect.class);
@Autowired
private TokenServiceCall tokenServiceCall;
@Value("${yzg.reqUrl:order,check,use}")
private String reqUrl;
@Value("${yzg.reqSize:5000}")
private int reqSize;
/**
* exec aop point aspect
*/
......@@ -66,8 +67,9 @@ public class WebAspect extends BaseRequestAspect {
long start = System.currentTimeMillis();
String name = joinPoint.getSignature().getName();
Ref<Boolean> flag = new Ref<>(false);
Exception ex = null;
try {
logger.info("[ {} ] request params is {}", name, joinPoint.getArgs());
Log.info(WebAspect.class, "[ %s ] request params is: %s", name, this.getMaxString(JsonHelper.serialize(joinPoint.getArgs())));
tokenServiceCall.tokenInit();
Object result = executeMethod(joinPoint, name);
......@@ -78,12 +80,10 @@ public class WebAspect extends BaseRequestAspect {
}
long end = System.currentTimeMillis();
logger.info("[ {} ] time ({})ms, result is {}", name, (end - start), responseResult);
tokenFinish(flag);
return result;
} catch (Exception e) {
long end = System.currentTimeMillis();
logger.error("[ {} ] time ({})ms, is error {}", name, (end - start), e);
ex = e;
responseResult = ExceptionHelper.getError(e);
if (getReturnType(joinPoint).getTypeName().indexOf(ResponseResult.class.getName()) > -1) {
return responseResult;
......@@ -92,9 +92,31 @@ public class WebAspect extends BaseRequestAspect {
}
} finally {
tokenFinish(flag);
long time = System.currentTimeMillis() - start;
if (ex != null) {
Log.error(WebAspect.class, "[ %s ] time %d ms, error: %s", name, time, getMaxString(ex.getMessage()));
} else {
Log.info(WebAspect.class, "[ %s ] time %d ms, result is: %s", name, time, getMaxString(JsonHelper.serialize(responseResult)));
}
Log.threadCommit();
saveInterLogs(joinPoint, responseResult);
saveInterLogs(joinPoint, responseResult, time);
}
}
/**
* 获取JSON,当Json过长时,截断
*
* @param paraJson
* @return
*/
private String getMaxString(String paraJson) {
if (paraJson != null && paraJson.length() > reqSize) {
paraJson = paraJson.substring(0, reqSize);
}
return paraJson;
}
/**
......@@ -196,7 +218,7 @@ public class WebAspect extends BaseRequestAspect {
* @param joinPoint 请求参数
* @param responseResult 返回参数
*/
private void saveInterLogs(ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
private void saveInterLogs(ProceedingJoinPoint joinPoint, ResponseResult responseResult, long time) {
// 日志请求不记录,防止死循环递归
if (applicationName.indexOf(CloudContans.LOG_MODULE) >= 0) {
return;
......@@ -208,6 +230,7 @@ public class WebAspect extends BaseRequestAspect {
return;
}
LogVo logVo = initLogInterVo(HttpAspectUtil.getHttpRequestUrl(), joinPoint, responseResult);
logVo.setUseTime((int) time);
addLog(logVo);
}
}
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