deason 4 éve
szülő
commit
2573ee04bb

+ 9 - 9
src/main/java/cn/com/qmth/examcloud/ws/starter/core/WebSocketHelper.java

@@ -11,10 +11,10 @@ import com.google.common.collect.Maps;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.api.commons.security.bean.UserType;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
 import cn.com.qmth.examcloud.commons.util.StringUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * WebSocket helper
@@ -25,7 +25,7 @@ import cn.com.qmth.examcloud.commons.util.StringUtil;
  */
 public class WebSocketHelper {
 
-	private static final ExamCloudLog WS_LOG = ExamCloudLogFactory.getLog("WS_LOGGER");
+	private static final Logger LOG = LoggerFactory.getLogger(WebSocketHelper.class);
 
 	private static final Map<String, Session> INDEX_MAP = Maps.newConcurrentMap();
 
@@ -132,12 +132,12 @@ public class WebSocketHelper {
 		try {
 			out.setDate(new Date());
 			String message = JsonUtil.toJson(out);
-			if (WS_LOG.isDebugEnabled()) {
-				WS_LOG.debug("[sendText]. path=" + path + "; message=" + message);
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("[sendText]. path=" + path + "; message=" + message);
 			}
 			session.getBasicRemote().sendText(message);
 		} catch (Exception e) {
-			WS_LOG.error("[sendText-FAIL]. path=" + path, e);
+			LOG.error("[sendText-FAIL]. path=" + path, e);
 			IOUtils.closeQuietly(session);
 			return;
 		}
@@ -153,12 +153,12 @@ public class WebSocketHelper {
 	 */
 	public static void sendText(Session session, String path, String out) {
 		try {
-			if (WS_LOG.isDebugEnabled()) {
-				WS_LOG.error("[sendText]. path=" + path + "; message=" + out);
+			if (LOG.isDebugEnabled()) {
+				LOG.error("[sendText]. path=" + path + "; message=" + out);
 			}
 			session.getBasicRemote().sendText(out);
 		} catch (Exception e) {
-			WS_LOG.error("[sendText-FAIL]. path=" + path, e);
+			LOG.error("[sendText-FAIL]. path=" + path, e);
 			IOUtils.closeQuietly(session);
 			return;
 		}

+ 13 - 13
src/main/java/cn/com/qmth/examcloud/ws/starter/core/WebSocketServerEndpoint.java

@@ -19,6 +19,8 @@ import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.logging.log4j.ThreadContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import com.google.gson.JsonElement;
@@ -27,8 +29,6 @@ import com.google.gson.JsonSyntaxException;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.support.SpringContextHolder;
@@ -44,7 +44,7 @@ import cn.com.qmth.examcloud.web.support.SpringContextHolder;
 @Component
 public class WebSocketServerEndpoint {
 
-	private static final ExamCloudLog WS_LOG = ExamCloudLogFactory.getLog("WS_LOGGER");
+	private static final Logger LOG = LoggerFactory.getLogger(WebSocketServerEndpoint.class);
 
 	private static RedisClient redisClient;
 
@@ -68,8 +68,8 @@ public class WebSocketServerEndpoint {
 
 		ThreadContext.put("TRACE_ID", sessionId);
 
-		if (WS_LOG.isDebugEnabled()) {
-			WS_LOG.debug("[onOpen]. path=" + path + "; sessionsAmount=" + amount);
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("[onOpen]. path=" + path + "; sessionsAmount=" + amount);
 		}
 
 		MessageOut out = new MessageOut(path, sessionId);
@@ -87,12 +87,12 @@ public class WebSocketServerEndpoint {
 		String token = getRequestParameter(session, "token");
 
 		if (StringUtils.isBlank(key)) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". key is blank");
+			LOG.error("[onOpen-FAIL]. path=" + path + ". key is blank");
 			IOUtils.closeQuietly(session);
 			return;
 		}
 		if (StringUtils.isBlank(token)) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is blank");
+			LOG.error("[onOpen-FAIL]. path=" + path + ". token is blank");
 			IOUtils.closeQuietly(session);
 			return;
 		}
@@ -150,7 +150,7 @@ public class WebSocketServerEndpoint {
 			ThreadContext.put("TRACE_ID", this.sessionId);
 		}
 
-		WS_LOG.debug("[onClose]. path=" + path + "; sessionsAmount=" + amount);
+		LOG.debug("[onClose]. path=" + path + "; sessionsAmount=" + amount);
 		WebSocketHelper.closeSession(session);
 	}
 
@@ -172,8 +172,8 @@ public class WebSocketServerEndpoint {
 			return;
 		}
 
-		if (WS_LOG.isDebugEnabled()) {
-			WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
+		if (LOG.isDebugEnabled()) {
+			LOG.debug("[onMessage]. path=" + path + ". message=" + message);
 		}
 
 		SessionInfo si = WebSocketHelper.getSessionInfo(session);
@@ -236,10 +236,10 @@ public class WebSocketServerEndpoint {
 			out.setContent(result);
 
 		} catch (StatusException e) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
+			LOG.error("[onMessage-FAIL]. path=" + path + "", e);
 			out.setStatus(e.getCode(), e.getDesc());
 		} catch (Exception e) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
+			LOG.error("[onMessage-FAIL]. path=" + path + "", e);
 			out.setStatus("500", "系统异常");
 		}
 
@@ -257,7 +257,7 @@ public class WebSocketServerEndpoint {
 			ThreadContext.put("TRACE_ID", this.sessionId);
 		}
 
-		WS_LOG.error("[onError]. path=" + path, t);
+		LOG.error("[onError]. path=" + path, t);
 		WebSocketHelper.closeSession(session);
 	}
 

+ 18 - 61
src/main/resources/log4j2.xml

@@ -2,65 +2,32 @@
 <Configuration status="WARN" monitorInterval="30">
 
     <Properties>
-        <Property name="commonLevel" value="${sys:log.commonLevel}"/>
-        <Property name="logPattern">
+        <Property name="LOG_LEVEL" value="${sys:log.commonLevel}"/>
+        <Property name="LOG_DIR" value="./logs/examcloud-ws"/>
+        <Property name="LOG_PATTERN">
             %d{yyyy-MM-dd HH:mm:ss.SSS} | %clr{%level} | %X{TRACE_ID} %X{CALLER} | %clr{%c{1.1}:%L}{cyan} | %m%n
         </Property>
     </Properties>
 
     <Appenders>
-        <!-- 控制台 日志 -->
-        <Console name="Console" target="SYSTEM_OUT">
-            <PatternLayout pattern="${logPattern}" charset="UTF-8"/>
+        <Console name="CONSOLE_APPENDER" target="SYSTEM_OUT">
+            <PatternLayout pattern="${LOG_PATTERN}" charset="UTF-8"/>
         </Console>
 
-        <!-- debug 日志 -->
-        <RollingFile name="DEBUG_APPENDER"
-                     fileName="./logs/debug/debug.log"
-                     filePattern="./logs/debug/debug-%d{yyyy.MM.dd.HH}-%i.log">
-            <PatternLayout pattern="${logPattern}" charset="UTF-8"/>
-            <Policies>
-                <TimeBasedTriggeringPolicy interval="1" modulate="false"/>
-                <SizeBasedTriggeringPolicy size="100 MB"/>
-            </Policies>
-            <DefaultRolloverStrategy max="1000">
-                <Delete basePath="./logs/debug" maxDepth="1">
-                    <IfFileName glob="debug-*.log">
-                        <IfAccumulatedFileSize exceeds="2 GB"/>
-                    </IfFileName>
-                </Delete>
-            </DefaultRolloverStrategy>
-        </RollingFile>
+        <RollingFile name="FILE_APPENDER"
+                     fileName="${LOG_DIR}/debug.log"
+                     filePattern="${LOG_DIR}/debug-%d{yyyyMMdd}-%i.log">
+            <PatternLayout pattern="${LOG_PATTERN}" charset="UTF-8"/>
 
-        <!-- 接口日志 -->
-        <RollingFile name="INTERFACE_APPENDER" fileName="./logs/interface/interface.log"
-                     filePattern="./logs/interface/interface-%d{yyyy.MM.dd.HH}-%i.log">
-            <PatternLayout pattern="${logPattern}" charset="UTF-8"/>
             <Policies>
                 <TimeBasedTriggeringPolicy interval="1" modulate="false"/>
                 <SizeBasedTriggeringPolicy size="100 MB"/>
             </Policies>
-            <DefaultRolloverStrategy max="1000">
-                <Delete basePath="./logs/interface" maxDepth="1">
-                    <IfFileName glob="interface-*.log">
-                        <IfAccumulatedFileSize exceeds="10 GB"/>
-                    </IfFileName>
-                </Delete>
-            </DefaultRolloverStrategy>
-        </RollingFile>
 
-        <!-- WebSocket 日志 -->
-        <RollingFile name="WS_APPENDER" fileName="./logs/ws/ws.log"
-                     filePattern="./logs/ws/ws-%d{yyyy.MM.dd.HH}-%i.log">
-            <PatternLayout pattern="${logPattern}" charset="UTF-8"/>
-            <Policies>
-                <TimeBasedTriggeringPolicy interval="1" modulate="false"/>
-                <SizeBasedTriggeringPolicy size="100 MB"/>
-            </Policies>
             <DefaultRolloverStrategy max="1000">
-                <Delete basePath="./logs/ws" maxDepth="1">
-                    <IfFileName glob="ws-*.log">
-                        <IfAccumulatedFileSize exceeds="2 GB"/>
+                <Delete basePath="${LOG_DIR}" maxDepth="1">
+                    <IfFileName glob="debug-*.log">
+                        <IfAccumulatedFileSize exceeds="10 GB"/>
                     </IfFileName>
                 </Delete>
             </DefaultRolloverStrategy>
@@ -84,24 +51,14 @@
         <!--<logger name="org.springframework.data.mongodb" level="DEBUG"/>-->
         <!--<logger name="org.springframework.data.redis" level="DEBUG"/>-->
 
-        <Logger name="cn.com.qmth" level="${commonLevel}" additivity="false">
-            <AppenderRef ref="DEBUG_APPENDER"/>
-            <AppenderRef ref="Console"/>
-        </Logger>
-
-        <Logger name="INTERFACE_LOGGER" level="${commonLevel}" additivity="false">
-            <AppenderRef ref="INTERFACE_APPENDER"/>
-            <AppenderRef ref="Console"/>
-        </Logger>
-
-        <Logger name="WS_LOGGER" level="${commonLevel}" additivity="false">
-            <AppenderRef ref="WS_APPENDER"/>
-            <AppenderRef ref="Console"/>
+        <Logger name="cn.com.qmth" level="${LOG_LEVEL}" additivity="false">
+            <AppenderRef ref="CONSOLE_APPENDER"/>
+            <AppenderRef ref="FILE_APPENDER"/>
         </Logger>
 
-        <Root level="${commonLevel}">
-            <AppenderRef ref="Console"/>
-            <AppenderRef ref="DEBUG_APPENDER"/>
+        <Root level="${LOG_LEVEL}">
+            <AppenderRef ref="CONSOLE_APPENDER"/>
+            <AppenderRef ref="FILE_APPENDER"/>
         </Root>
     </Loggers>