From 44a3bb69efaa5e4304be4a9d55daa8de799b9bd2 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 26 Apr 2023 10:56:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=97=A5=E5=BF=97=E6=8C=89?= =?UTF-8?q?=E7=85=A7=E6=A8=A1=E5=9D=97=E7=94=9F=E6=88=90=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/main/resources/logback-spring.xml | 16 +- .../io/metersphere/sdk/util/LogUtils.java | 144 ++++++++++++++---- .../api/jmeter/JMeterLoggerAppender.java | 5 +- .../{utils => util}/FixedCapacityUtils.java | 2 +- .../io/metersphere/api/utils/ApiLogUtils.java | 93 ----------- .../metersphere/load/util/LoadLogUtils.java | 93 ----------- .../project/util/ProjectLogUtils.java | 87 ----------- .../system/util/SystemLogUtils.java | 93 ----------- .../metersphere/plan/util/PlanLogUtils.java | 93 ----------- .../metersphere/track/util/TrackLogUtils.java | 93 ----------- .../io/metersphere/ui/util/UiLogUtils.java | 93 ----------- .../utils/WorkstationLogUtils.java | 93 ----------- 12 files changed, 127 insertions(+), 778 deletions(-) rename backend/services/api-test/src/main/java/io/metersphere/api/{utils => util}/FixedCapacityUtils.java (97%) delete mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiLogUtils.java delete mode 100644 backend/services/load-test/src/main/java/io/metersphere/load/util/LoadLogUtils.java delete mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/util/ProjectLogUtils.java delete mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/util/SystemLogUtils.java delete mode 100644 backend/services/test-plan/src/main/java/io/metersphere/plan/util/PlanLogUtils.java delete mode 100644 backend/services/test-track/src/main/java/io/metersphere/track/util/TrackLogUtils.java delete mode 100644 backend/services/ui-test/src/main/java/io/metersphere/ui/util/UiLogUtils.java delete mode 100644 backend/services/workstation/src/main/java/io/metersphere/workstation/utils/WorkstationLogUtils.java diff --git a/backend/app/src/main/resources/logback-spring.xml b/backend/app/src/main/resources/logback-spring.xml index 86a5eb823c..e8361757d4 100644 --- a/backend/app/src/main/resources/logback-spring.xml +++ b/backend/app/src/main/resources/logback-spring.xml @@ -149,7 +149,7 @@ 10000 - + @@ -177,7 +177,7 @@ 10000 - + @@ -206,7 +206,7 @@ 10000 - + @@ -236,7 +236,7 @@ 10000 - + @@ -265,7 +265,7 @@ 10000 - + @@ -293,7 +293,7 @@ 10000 - + @@ -322,7 +322,7 @@ 10000 - + @@ -351,7 +351,7 @@ 10000 - + diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java index 16da5267b2..6b4652bb30 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java @@ -2,129 +2,174 @@ package io.metersphere.sdk.util; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; public class LogUtils { - //日志工具类 public static final String DEBUG = "DEBUG"; public static final String INFO = "INFO"; public static final String WARN = "WARN"; public static final String ERROR = "ERROR"; - public static void writeLog(Object msg, String level, Logger logger) { - if (logger != null && logger.isInfoEnabled()) { - switch (level) { - case DEBUG -> logger.debug(LogUtils.getMsg(msg)); - case INFO -> logger.info(LogUtils.getMsg(msg)); - case WARN -> logger.warn(LogUtils.getMsg(msg)); - case ERROR -> logger.error(LogUtils.getMsg(msg)); - default -> logger.error(StringUtils.EMPTY); + /** + * 初始化日志 + * + * @return + */ + public static Logger getLogger() { + return LoggerFactory.getLogger(LogUtils.getLogClass()); + } + + public static void writeLog(Object msg, String level) { + Logger logger = LogUtils.getLogger(); + + if (DEBUG.equals(level)) { + if (logger != null && logger.isDebugEnabled()) { + logger.debug(LogUtils.getMsg(msg)); + } + } else if (INFO.equals(level)) { + if (logger != null && logger.isInfoEnabled()) { + logger.info(LogUtils.getMsg(msg)); + } + } else if (WARN.equals(level)) { + if (logger != null && logger.isWarnEnabled()) { + logger.warn(LogUtils.getMsg(msg)); + } + } else if (ERROR.equals(level)) { + if (logger != null && logger.isErrorEnabled()) { + logger.error(LogUtils.getMsg(msg)); + } + } else { + if (logger != null && logger.isErrorEnabled()) { + logger.error(StringUtils.EMPTY); } } } - public static void info(Object msg, Logger logger) { + public static void info(Object msg) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isInfoEnabled()) { logger.info(LogUtils.getMsg(msg)); } } - public static void info(Object msg, Object o1, Logger logger) { + public static void info(Object msg, Object o1) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isInfoEnabled()) { logger.info(LogUtils.getMsg(msg), o1); } } - public static void info(Object msg, Object o1, Object o2, Logger logger) { + public static void info(Object msg, Object o1, Object o2) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isInfoEnabled()) { logger.info(LogUtils.getMsg(msg), o1, o2); } } - public static void info(Object msg, Object[] obj, Logger logger) { + public static void info(Object msg, Object[] obj) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isInfoEnabled()) { logger.info(LogUtils.getMsg(msg), obj); } } - public static void debug(Object msg, Logger logger) { + public static void debug(Object msg) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isDebugEnabled()) { logger.debug(LogUtils.getMsg(msg)); } } - public static void debug(Object msg, Object o, Logger logger) { + public static void debug(Object msg, Object o) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isDebugEnabled()) { logger.debug(LogUtils.getMsg(msg), o); } } - public static void debug(Object msg, Object o1, Object o2, Logger logger) { + public static void debug(Object msg, Object o1, Object o2) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isDebugEnabled()) { logger.debug(LogUtils.getMsg(msg), o1, o2); } } - public static void debug(Object msg, Object[] obj, Logger logger) { + public static void debug(Object msg, Object[] obj) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isDebugEnabled()) { logger.debug(LogUtils.getMsg(msg), obj); } } - public static void warn(Object msg, Logger logger) { + public static void warn(Object msg) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isWarnEnabled()) { logger.warn(LogUtils.getMsg(msg)); } } - public static void warn(Object msg, Object o, Logger logger) { + public static void warn(Object msg, Object o) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isWarnEnabled()) { logger.warn(LogUtils.getMsg(msg), o); } } - public static void warn(Object msg, Object o1, Object o2, Logger logger) { + public static void warn(Object msg, Object o1, Object o2) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isWarnEnabled()) { logger.warn(LogUtils.getMsg(msg), o1, o2); } } - public static void warn(Object msg, Object[] obj, Logger logger) { + public static void warn(Object msg, Object[] obj) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isWarnEnabled()) { logger.warn(LogUtils.getMsg(msg), obj); } } - public static void error(Object msg, Logger logger) { + public static void error(Object msg) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(msg));// 并追加方法名称 } } - public static void error(Throwable e, Logger logger) { + public static void error(Throwable e) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(e), e);// 同时打印错误堆栈信息 } } - public static void error(Object msg, Object o, Logger logger) { + public static void error(Object msg, Object o) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(msg), o); } } - public static void error(Object msg, Object o1, Object o2, Logger logger) { + public static void error(Object msg, Object o1, Object o2) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(msg), o1, o2); } } - public static void error(Object msg, Object[] obj, Logger logger) { + public static void error(Object msg, Object[] obj) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(msg), obj); } } - public static void error(Object msg, Throwable ex, Logger logger) { + public static void error(Object msg, Throwable ex) { + Logger logger = LogUtils.getLogger(); if (logger != null && logger.isErrorEnabled()) { logger.error(LogUtils.getMsg(msg), ex); } @@ -134,7 +179,7 @@ public class LogUtils { String str = ""; if (msg != null) { - str = LogUtils.getLogMethod() + "[" + msg + "]"; + str = LogUtils.getLogMethod() + "[" + msg.toString() + "]"; } else { str = LogUtils.getLogMethod() + "[null]"; } @@ -149,6 +194,22 @@ public class LogUtils { return LogUtils.getMsg(msg, null); } + /** + * 得到调用类名称 + * + * @return + */ + private static String getLogClass() { + String str = ""; + + StackTraceElement[] stack = (new Throwable()).getStackTrace(); + if (stack.length > 3) { + StackTraceElement ste = stack[3]; + str = ste.getClassName();// 类名称 + } + + return str; + } /** * 得到调用方法名称 @@ -157,11 +218,38 @@ public class LogUtils { */ private static String getLogMethod() { String str = ""; + StackTraceElement[] stack = (new Throwable()).getStackTrace(); if (stack.length > 4) { StackTraceElement ste = stack[4]; str = "Method[" + ste.getMethodName() + "]";// 方法名称 } + return str; } + + public static String toString(Throwable e) { + + try (StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw);) { + //将出错的栈信息输出到printWriter中 + e.printStackTrace(pw); + pw.flush(); + sw.flush(); + return sw.toString(); + } catch (IOException ex) { + return ex.getMessage(); + } + } + + public static String getExceptionDetailsToStr(Exception e) { + StringBuilder sb = new StringBuilder(e.toString()); + StackTraceElement[] stackElements = e.getStackTrace(); + for (StackTraceElement stackTraceElement : stackElements) { + sb.append(stackTraceElement.toString()); + sb.append(StringUtils.LF); + } + sb.append(StringUtils.LF); + return sb.toString(); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/jmeter/JMeterLoggerAppender.java b/backend/services/api-test/src/main/java/io/metersphere/api/jmeter/JMeterLoggerAppender.java index 6cd7db646a..831e990105 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/jmeter/JMeterLoggerAppender.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/jmeter/JMeterLoggerAppender.java @@ -3,8 +3,7 @@ package io.metersphere.api.jmeter; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.StackTraceElementProxy; import ch.qos.logback.core.UnsynchronizedAppenderBase; -import io.metersphere.api.utils.ApiLogUtils; -import io.metersphere.api.utils.FixedCapacityUtils; +import io.metersphere.api.util.FixedCapacityUtils; import io.metersphere.sdk.util.DateUtils; import io.metersphere.sdk.util.LogUtils; import org.apache.commons.lang3.StringUtils; @@ -38,7 +37,7 @@ public class JMeterLoggerAppender extends UnsynchronizedAppenderBase