作者 朱兆平

启用websockt在线查看日志文件

... ... @@ -30,6 +30,15 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--有WEBSOCKET包 包含了spring-boot-starter-web 和spring-boot-starter包 有这个包不要引入这俩包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
... ... @@ -40,16 +49,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>ch.qos.logback</groupId>-->
<!--<artifactId>logback-classic</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
<!--</dependency>-->
<!--spring boot-->
<!-- alibaba的druid数据库连接池 -->
<dependency>
... ...
package com.tianbo.warehouse.bean;
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() {
return new ServerEndpointExporter();
}
}
... ...
package com.tianbo.warehouse.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ImfLog {
@RequestMapping("/log/imf")
public String IMFlog(){
return "log/imf";
}
}
... ...
package com.tianbo.warehouse.controller;
import com.tianbo.warehouse.handle.IO_Log_Handle;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint(value = "/log")
@Component
public class Log {
private Process process;
private InputStream inputStream;
//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<Log> webSocketSet = new CopyOnWriteArraySet<Log>();
@OnOpen
public void onOpen(Session session){
this.session = session;
webSocketSet.add(this);
addOnlineCount();
try {
process = Runtime.getRuntime().exec("tail -f logs/imf.log");
inputStream = process.getInputStream();
IO_Log_Handle thread = new IO_Log_Handle(inputStream, session);
thread.start();
}catch (IOException e){
e.printStackTrace();
}
}
/**
* WebSocket请求关闭
*/
@OnClose
public void onClose() {
try {
if(inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if(process != null){
process.destroy();
}
}
@OnError
public void onError(Throwable thr) {
thr.printStackTrace();
}
public static synchronized void addOnlineCount() {
Log.onlineCount++;
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
}
... ...
package com.tianbo.warehouse.handle;
import javax.websocket.Session;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class IO_Log_Handle extends Thread{
private BufferedReader reader;
private Session session;
public IO_Log_Handle(InputStream in, Session session) {
this.reader = new BufferedReader(new InputStreamReader(in));
this.session = session;
}
@Override
public void run() {
String line;
try {
while((line = reader.readLine()) != null) {
// 将实时日志通过WebSocket发送给客户端,给每一行添加一个HTML换行
session.getBasicRemote().sendText(line + "<br>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
... ...
... ... @@ -24,7 +24,7 @@ public class IMF_Task {
public static String isNeedSend = "N";
@Scheduled(fixedRate = 5000)
//@Scheduled(fixedRate = 5000)
private static void start() throws Exception {
PropertyConfigurator.configure("config/log4j.properties");
client = IMFClientFactory.createInstance();
... ...
此 diff 太大无法显示。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tail log</title>
<script src="/js/jquery.js"></script>
</head>
<body>
<div id="log-container" style="height: 450px; overflow-y: scroll; background: #333; color: #aaa; padding: 10px;">
<div>
</div>
</div>
</body>
<script>
$(document).ready(function() {
// 指定websocket路径
var websocket = new WebSocket('ws://localhost:7003/log');
websocket.onmessage = function(event) {
// 接收服务端的实时日志并添加到HTML页面中
$("#log-container div").append(event.data);
// 滚动条滚动到最低部
$("#log-container").scrollTop($("#log-container div").height() - $("#log-container").height());
};
});
</script>
</body>
</html>
\ No newline at end of file
... ...