Commit 24e87bbb authored by yanzg's avatar yanzg

修复等待时间

parent 6dcdfdc0
......@@ -62,7 +62,7 @@ public class Log {
*/
public static void info(Class<?> cls, String msg, Object... args) {
String toMsg = getFormat(msg, args);
add(new LogInfo(cls, new Date(), null, toMsg));
add(new LogInfo(cls, new Date(), false, null, toMsg));
}
/**
......
......@@ -6,10 +6,8 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
......@@ -25,14 +23,16 @@ public class AspectLogResult {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final AspectLogBody aspectLogBody;
private final AspectLogTime aspectLogTime;
private final ResponseResult<Object> responseDefault = new ResponseResult();
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody) {
public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, AspectLogTime aspectLogTime) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody;
this.aspectLogTime = aspectLogTime;
}
/**
......@@ -52,6 +52,8 @@ public class AspectLogResult {
public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
Object requestBody, Object result, Exception resultEx, LogVo log) {
try {
String fullUrl = String.format("%s:%s", tag, url);
// ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()));
if (StringHelper.isEmpty(log.getLogId())) {
return;
......@@ -61,8 +63,9 @@ public class AspectLogResult {
log.setContentTo(body);
}
long time = System.currentTimeMillis() - start;
boolean isLogDisplay = (logFlag && this.cloudConfig.isLogCommon()) || resultEx != null;
boolean isLogCommon = logFlag && this.cloudConfig.isLogCommon();
boolean isLogDatabase = resultEx != null;
boolean isLogDisplay = isLogCommon || isLogDatabase;
// 处理结果
ResponseResult responseResult = responseDefault;
if (result instanceof ResponseResult) {
......@@ -70,19 +73,18 @@ public class AspectLogResult {
} else if (result != null) {
responseResult = ResponseResult.result(result);
}
if (isLogDisplay || isLogDatabase) {
if (isLogDisplay) {
// 执行时间
if (resultEx != null) {
responseResult = ExceptionHelper.getError(resultEx);
}
String json = JsonHelper.serialize(responseResult);
if (isLogDisplay) {
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
}
Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
if (isLogDatabase) {
logLocal.result(log, responseResult.getCode(), json);
}
}
aspectLogTime.finish(fullUrl, time, isLogDatabase);
} catch (Exception e) {
e.printStackTrace();
} finally {
......
......@@ -17,11 +17,13 @@ public class AspectLogStart {
private final CloudConfig cloudConfig;
private final LogLocal logLocal;
private final AspectLogBody aspectLogBody;
private final AspectLogTime aspectLogTime;
public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody) {
public AspectLogStart(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, AspectLogTime aspectLogTime) {
this.cloudConfig = cloudConfig;
this.logLocal = logLocal;
this.aspectLogBody = aspectLogBody;
this.aspectLogTime = aspectLogTime;
}
/**
......@@ -37,6 +39,9 @@ public class AspectLogStart {
// @Async
public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) {
try {
String fullUrl = String.format("%s:%s", tag, url);
aspectLogTime.start(fullUrl);
String body = aspectLogBody.getBodyString(requestBody);
if (logFlag) {
this.logLocal.startLog(log, tag, String.format("%s:%s", this.cloudConfig.getApplicationName(), url), body);
......
......@@ -7,7 +7,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
......@@ -26,6 +25,50 @@ public class AspectLogTime implements ThreadNext.Next, InitializingBean {
ThreadNext.start(this, "aspectLogTime");
}
/**
* 获取缓存对象
*
* @param url 地址
* @return 缓存对象
*/
private AspectUrlCountVo getCount(String url) {
AspectUrlCountVo ret = memoryCache.get(url);
if (ret != null) {
return ret;
}
synchronized (memoryCache) {
ret = memoryCache.get(url);
if (ret != null) {
return ret;
}
ret = new AspectUrlCountVo(url);
memoryCache.put(url, ret);
return ret;
}
}
/**
* 开始记录日志
*
* @param url 请求地址
*/
public void start(String url) {
AspectUrlCountVo count = getCount(url);
count.addStart();
}
/**
* 结束
*
* @param url  请求地址
* @param time 执行时间
* @param isLog 是否记录日志
*/
public void finish(String url, long time, boolean isLog) {
AspectUrlCountVo count = getCount(url);
count.addFinish(time, isLog);
}
@Override
public boolean next() {
List<AspectUrlCountVo> rowList = new ArrayList<>(memoryCache.getValues());
......@@ -39,6 +82,6 @@ public class AspectLogTime implements ThreadNext.Next, InitializingBean {
@Override
public int getNextTime() {
return 60 * 1000;
return 5 * 60 * 1000;
}
}
......@@ -117,7 +117,7 @@ public class AspectLogUrl {
}
} else {
HttpServletRequest request = CookiesHelper.getRequest();
sb.append(request.getRequestURL());
sb.append(request.getRequestURI());
if (!StringHelper.isEmpty(request.getQueryString())) {
sb.append("?");
sb.append(request.getQueryString());
......
......@@ -6,18 +6,41 @@ package com.yanzuoguang.cloud.aop;
* @author 颜佐光
*/
public class AspectUrlCountVo {
private final Object lockStart = new Object();
private final Object lockFinish = new Object();
private volatile String url;
private volatile int startCount;
private volatile int endCount;
private volatile int logCount;
private volatile long totalTime;
private volatile int avgTime;
private volatile int minTime;
private volatile int maxTime;
private volatile long avgTime;
private volatile long minTime = Long.MAX_VALUE;
private volatile long maxTime;
public AspectUrlCountVo(String url) {
this.url = url;
}
public void addStart() {
synchronized (this.lockStart) {
this.startCount++;
}
}
public void addFinish(long time, boolean isLog) {
synchronized (lockFinish) {
this.endCount++;
if (isLog) {
this.logCount++;
}
this.totalTime += this.endCount;
this.minTime = Math.min(time, this.minTime);
this.maxTime = Math.min(time, this.maxTime);
this.avgTime = this.totalTime / this.endCount;
}
}
public String getUrl() {
return url;
}
......@@ -42,6 +65,14 @@ public class AspectUrlCountVo {
this.endCount = endCount;
}
public int getLogCount() {
return logCount;
}
public void setLogCount(int logCount) {
this.logCount = logCount;
}
public long getTotalTime() {
return totalTime;
}
......@@ -50,27 +81,27 @@ public class AspectUrlCountVo {
this.totalTime = totalTime;
}
public int getAvgTime() {
public long getAvgTime() {
return avgTime;
}
public void setAvgTime(int avgTime) {
public void setAvgTime(long avgTime) {
this.avgTime = avgTime;
}
public int getMinTime() {
public long getMinTime() {
return minTime;
}
public void setMinTime(int minTime) {
public void setMinTime(long minTime) {
this.minTime = minTime;
}
public int getMaxTime() {
public long getMaxTime() {
return maxTime;
}
public void setMaxTime(int maxTime) {
public void setMaxTime(long maxTime) {
this.maxTime = maxTime;
}
}
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