支持服务端日志传入前端

This commit is contained in:
648540858
2024-10-31 17:37:34 +08:00
parent 376f14733e
commit effb705f99
7 changed files with 244 additions and 2 deletions

View File

@@ -0,0 +1,66 @@
package com.genersoft.iot.vmp.conf.webLog;
import lombok.extern.slf4j.Slf4j;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ServerEndpoint(value = "/channel/log")
@Slf4j
public class LogChannel {
public static final ConcurrentMap<String, LogChannel> CHANNELS = new ConcurrentHashMap<>();
private Session session;
@OnMessage(maxMessageSize = 1) // MaxMessage 1 byte
public void onMessage(String message) {
log.debug("Recv Message: {}", message);
try {
this.session.close(new CloseReason(CloseReason.CloseCodes.TOO_BIG, "此节点不接收任何客户端信息"));
} catch (IOException e) {
log.error("[Web-Log] 连接关闭失败: id={}, err={}", this.session.getId(), e.getMessage());
}
}
@OnOpen
public void onOpen(Session session, EndpointConfig endpointConfig) {
this.session = session;
this.session.setMaxIdleTimeout(0);
CHANNELS.put(this.session.getId(), this);
log.info("[Web-Log] 连接已建立: id={}", this.session.getId());
}
@OnClose
public void onClose(CloseReason closeReason) {
log.info("[Web-Log] 连接已断开: id={}, err={}", this.session.getId(), closeReason);
CHANNELS.remove(this.session.getId());
}
@OnError
public void onError(Throwable throwable) throws IOException {
log.info("[Web-Log] 连接错误: id={}, err= ", this.session.getId(), throwable);
this.session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, throwable.getMessage()));
}
/**
* Push messages to all clients
*
* @param message
*/
public static void push(String message) {
CHANNELS.values().stream().forEach(endpoint -> {
if (endpoint.session.isOpen()) {
endpoint.session.getAsyncRemote().sendText(message);
}
});
}
}

View File

@@ -0,0 +1,22 @@
package com.genersoft.iot.vmp.conf.webLog;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import com.genersoft.iot.vmp.utils.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class WebSocketAppender extends AppenderBase<ILoggingEvent> {
private PatternLayoutEncoder encoder;
@Override
protected void append(ILoggingEvent loggingEvent) {
byte[] data = this.encoder.encode(loggingEvent);
// Push to client.
LogChannel.push(DateUtil.timestampMsTo_yyyy_MM_dd_HH_mm_ss(loggingEvent.getTimeStamp()) + " " + loggingEvent.getFormattedMessage());
}
}

View File

@@ -0,0 +1,19 @@
package com.genersoft.iot.vmp.conf.websocket;
import com.genersoft.iot.vmp.conf.webLog.LogChannel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
ServerEndpointExporter endpointExporter = new ServerEndpointExporter();
endpointExporter.setAnnotatedEndpointClasses(LogChannel.class);
return endpointExporter;
}
}