Commit d9a9521a authored by yanzg's avatar yanzg

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

parent 0c29ea54
package com.yanzuoguang.log;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.CloudConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 日志时间
......@@ -21,6 +22,8 @@ public class LogCountTime implements ThreadNext.Next, InitializingBean {
private static final String URL_PARA_START = "?";
private final LogConfig logConfig;
private final CloudConfig cloudConfig;
private final LogBase logBase;
/**
* 今日缓存,超过时间会自动清空
*/
......@@ -30,8 +33,10 @@ public class LogCountTime implements ThreadNext.Next, InitializingBean {
*/
private String today;
public LogCountTime(LogConfig logConfig) {
public LogCountTime(LogConfig logConfig, CloudConfig cloudConfig, LogBase logBase) {
this.logConfig = logConfig;
this.cloudConfig = cloudConfig;
this.logBase = logBase;
}
@Override
......@@ -58,15 +63,18 @@ public class LogCountTime implements ThreadNext.Next, InitializingBean {
* @param urlFrom 地址
* @return 缓存对象
*/
private LogUrlCountVo getCount(String urlFrom) {
private LogUrlCountVo getCount(String urlFrom, int level) {
// 初始化日期
initToday();
String key;
// 目标地址
String urlTo;
if (urlFrom.contains(URL_PARA_START)) {
key = urlFrom.substring(0, urlFrom.indexOf(URL_PARA_START));
urlTo = urlFrom.substring(0, urlFrom.indexOf(URL_PARA_START));
} else {
key = urlFrom;
urlTo = urlFrom;
}
// 关键字
String key = StringHelper.getId(urlTo, level);
// 缓存中获取对象
LogUrlCountVo ret = todayMemoryCache.get(key);
if (ret != null) {
......@@ -78,7 +86,7 @@ public class LogCountTime implements ThreadNext.Next, InitializingBean {
if (ret != null) {
return ret;
}
ret = new LogUrlCountVo(key);
ret = new LogUrlCountVo(key, level);
todayMemoryCache.put(key, ret);
return ret;
}
......@@ -90,24 +98,60 @@ public class LogCountTime implements ThreadNext.Next, InitializingBean {
* @param log 日志信息
*/
public void finish(LogInfoVo log) {
LogUrlCountVo count = getCount(log.getUrl());
count.addStart();
count.addFinish(log.getUseTime(), log.getStatus() == LogInfoVo.STATUS_ERROR);
if (log == null) {
return;
}
// 获取当前使用时间
long useTime;
if (log.getEnd() == 0) {
useTime = System.currentTimeMillis() - log.getStart();
} else {
useTime = log.getUseTime();
}
// 获取当前请求的级别
int level = 0;
if (useTime > cloudConfig.getLockTime()) {
level = -1;
} else {
// 这里需要时间按照升序排序
List<Integer> times = cloudConfig.getLogTimeMax();
for (Integer time : times) {
if (time < useTime) {
level = time;
}
}
}
// 设置当前等级
LogInfoVo.Handle handle = new LogInfoVo.Handle(level, useTime, log.getStatus() == LogInfoVo.STATUS_ERROR);
synchronized (log) {
// 非第一次处理
LogInfoVo.Handle hisHandle = log.getHandle();
if (hisHandle != null) {
// 当前合计对象
LogUrlCountVo count = getCount(log.getUrl(), hisHandle.getLevel());
count.subFinish(hisHandle.getUseTime(), hisHandle.isLog());
}
log.setHandle(handle);
// 当前合计对象
LogUrlCountVo count = getCount(log.getUrl(), handle.getLevel());
count.addFinish(handle.getUseTime(), handle.isLog());
}
}
@Override
public boolean next() {
LogCountResult todayResult = new LogCountResult(this.today, todayMemoryCache.getValues());
if (todayResult.getList().isEmpty()) {
List<LogUrlCountVo> list = todayResult.getList();
if (list.isEmpty()) {
return true;
}
// 按照使用总时间升序排序
todayResult.getList().sort(Comparator.comparingLong(LogUrlCountVo::getTotalTime));
// 先按照等级排序,然后再按照使用总时间升序排序
list.sort(Comparator.comparing(LogUrlCountVo::getLevel)
.thenComparing(LogUrlCountVo::getTotalTime));
// 逆转排序
Collections.reverse(todayResult.getList());
// 打印日志
String tag = todayResult.getTodayTime() + "执行次数:";
System.out.println(CollectionString.getCollectionString(tag, todayResult.getList()));
Collections.reverse(list);
// 今日合计
logBase.notifyTodayCount(todayResult);
// 下次继续执行
return true;
}
......
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