fix(接口自动化): 控制台日志处理

--bug=1007862 --user=赵勇 控制中台不显示任何信息 https://www.tapd.cn/55049933/s/1067922
This commit is contained in:
fit2-zhao 2021-11-15 19:27:52 +08:00 committed by fit2-zhao
parent 089545ed90
commit d453674d6c
3 changed files with 36 additions and 16 deletions

View File

@ -0,0 +1,31 @@
package io.metersphere.api.jmeter;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public class FixedCapacityUtils {
public static Map<Long, StringBuffer> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());
public static StringBuffer get(Long key) {
return fixedCapacityCache.get(key);
}
public static void put(Long key, StringBuffer value) {
fixedCapacityCache.put(key, value);
}
public static int size() {
return fixedCapacityCache.size();
}
static class LRUHashMap<K, V> extends LinkedHashMap<K, V> {
private int capacity = 100;
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}
}
}

View File

@ -6,18 +6,10 @@ import ch.qos.logback.core.UnsynchronizedAppenderBase;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import java.util.LinkedHashMap;
import java.util.Map;
public class JmeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
public static Map<Long, StringBuffer> logger;
@Override
public void append(ILoggingEvent event) {
try {
if (logger == null) {
logger = new LinkedHashMap<>();
}
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG)) {
StringBuffer message = new StringBuffer();
message.append(DateUtils.getTimeStr(event.getTimeStamp())).append(" ")
@ -34,10 +26,10 @@ public class JmeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEve
}
}
}
if (logger.containsKey(event.getTimeStamp())) {
logger.get(event.getTimeStamp()).append(message);
if (FixedCapacityUtils.fixedCapacityCache.containsKey(event.getTimeStamp())) {
FixedCapacityUtils.fixedCapacityCache.get(event.getTimeStamp()).append(message);
} else {
logger.put(event.getTimeStamp(), message);
FixedCapacityUtils.fixedCapacityCache.put(event.getTimeStamp(), message);
}
}
} catch (Exception e) {

View File

@ -148,7 +148,7 @@ public class MsResultService {
}
Long endTime = System.currentTimeMillis();
Long finalStartTime = startTime;
String logMessage = JmeterLoggerAppender.logger.entrySet().stream()
String logMessage = FixedCapacityUtils.fixedCapacityCache.entrySet().stream()
.filter(map -> map.getKey() > finalStartTime && map.getKey() < endTime)
.map(map -> map.getValue()).collect(Collectors.joining());
if (removed) {
@ -160,11 +160,8 @@ public class MsResultService {
}
FixedTask.tasks.remove(testId);
}
if (FixedTask.tasks.isEmpty()) {
JmeterLoggerAppender.logger.clear();
}
return logMessage;
}catch (Exception e){
} catch (Exception e) {
return "";
}
}