Commit f451bbd9 authored by yanzg's avatar yanzg

常规BUG的修改

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