refactor(接口测试): 切面日志调整

This commit is contained in:
fit2-zhao 2024-12-09 18:18:00 +08:00 committed by Craftsman
parent 233e2e973b
commit 2cc965f516
1 changed files with 31 additions and 13 deletions

View File

@ -9,29 +9,47 @@ import io.metersphere.commons.utils.FixedCapacityUtil;
import org.apache.commons.lang3.StringUtils;
public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private final static String THREAD_SPLIT = " ";
private static final String THREAD_SPLIT = " ";
private static final String SPACE = " ";
private static final String LINE_FEED = "\n";
private static final String UNKNOWN_HOST_EXCEPTION = "java.net.UnknownHostException";
@Override
public void append(ILoggingEvent event) {
try {
// Ensure the event level is not DEBUG and thread name is not empty
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG) && StringUtils.isNotEmpty(event.getThreadName())) {
StringBuilder message = new StringBuilder();
String threadName = StringUtils.substringBeforeLast(event.getThreadName(), THREAD_SPLIT);
message.append(DateUtils.getTimeStr(event.getTimeStamp())).append(StringUtils.SPACE)
.append(event.getLevel()).append(StringUtils.SPACE)
.append(event.getThreadName()).append(StringUtils.SPACE)
.append(event.getFormattedMessage()).append(StringUtils.LF);
StringBuilder message = new StringBuilder();
// Build the log message
message.append(DateUtils.getTimeStr(event.getTimeStamp()))
.append(SPACE)
.append(event.getLevel())
.append(SPACE)
.append(event.getThreadName())
.append(SPACE)
.append(event.getFormattedMessage())
.append(LINE_FEED);
// If there's a throwable associated with the event, append its details
if (event.getThrowableProxy() != null) {
message.append(event.getThrowableProxy().getMessage()).append(StringUtils.LF);
message.append(event.getThrowableProxy().getClassName()).append(StringUtils.LF);
if (event.getThrowableProxy().getStackTraceElementProxyArray() != null) {
for (StackTraceElementProxy stackTraceElementProxy : event.getThrowableProxy().getStackTraceElementProxyArray()) {
message.append(" ").append(stackTraceElementProxy.getSTEAsString()).append(StringUtils.LF);
message.append(event.getThrowableProxy().getMessage())
.append(LINE_FEED)
.append(event.getThrowableProxy().getClassName())
.append(LINE_FEED);
// Append stack trace if present
StackTraceElementProxy[] stackTraceElements = event.getThrowableProxy().getStackTraceElementProxyArray();
if (stackTraceElements != null) {
for (StackTraceElementProxy stackTraceElementProxy : stackTraceElements) {
message.append(" ").append(stackTraceElementProxy.getSTEAsString()).append(LINE_FEED);
}
}
}
if (!message.toString().contains("java.net.UnknownHostException") && FixedCapacityUtil.containsKey(threadName)) {
// Check for UnknownHostException and ensure thread is in FixedCapacityUtil before appending
if (!message.toString().contains(UNKNOWN_HOST_EXCEPTION) && FixedCapacityUtil.containsKey(threadName)) {
FixedCapacityUtil.get(threadName).append(message);
}
}
@ -39,4 +57,4 @@ public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEve
LogUtil.error(e);
}
}
}
}