springboot服务器信息,通过websocket即时预览springboot服务端的日志信息

通过websocket即时预览springboot服务端的日志信息

通过读取日志文件获取日志消息

这种方式,需要知道日志文本文件的地址。通过随机IO,读取到最新的日志文本数据。

// 不能打开写权限("w"),不然其他程序没法写入数据到该文件

try(RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\log.log", "r")){

// 最后一次指针的位置,默认从头开始

long lastPointer = 0;

// 每一行读取到的数据

String line = null;

// 持续监听文件

while(true) {

// 从最后一次读取到的数据开始读取

randomAccessFile.seek(lastPointer);

// 读取一行,遇到换行符停止,不包含换行符

while((line = randomAccessFile.readLine()) != null) {

System.out.print(line + "\n"); // 打印日志消息,或者响应给客户端

}

// 读取完毕后,记录最后一次读取的指针位置

lastPointer = randomAccessFile.getFilePointer();

}

}

通过自定义 Appender 获取到最新的消息

这种方式使用的日志框架为 logback,其实我对logback也不熟悉,只是看ch.qos.logback.core.ConsoleAppender 获得灵感,想着通过修改 outputStream 熟悉,把日志输出到自定义的管道流,再从管道流中读取到最新的日志消息,推送给客户端。

自定义 Appender 的实现

覆写 start 方法,在开始之前,通过 setOutputStream 设置管道流,并且启动一个线程,不停的从管道读取流中获取到最新的数据

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

import java.nio.charset.StandardCharsets;

import javax.websocket.Session;

import ch.qos.logback.core.OutputStreamAppender;

import io.springboot.log.web.socket.channel.LogChannel;

public class SocketOutputStreamAppender extends OutputStreamAppender {

@Override

public void start() {

// 管道读取流

PipedInputStream pipedInputStream = new PipedInputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pipedInputStream, StandardCharsets.UTF_8));

// 管道写入流

PipedOutputStream pipedOutputStream = new PipedOutputStream();

try {

// 写入读取管道链接

pipedOutputStream.connect(pipedInputStream);

} catch (IOException e) {

e.printStackTrace();

}

// 设置输出流到Appender

super.setOutputStream(pipedOutputStream);

// 子线程开始从管道流读取数据

Thread thread = new Thread(() -> {

String line = null;

while (true) {

try {

line = bufferedReader.readLine();

} catch (IOException e) {

// e.printStackTrace();

}

if (line != null) {

for (Session channel : LogChannel.CHANNELS.values()) {

if (channel.isOpen()) {

// 异步传输数据到客户端

channel.getAsyncRemote().sendText(line);

}

}

}

}

});

thread.setDaemon(Boolean.TRUE);

thread.setName("socket-log");

thread.start();

super.start();

}

}

logback的配置

${CONSOLE_LOG_PATTERN}

${CONSOLE_LOG_PATTERN}

最终的效果

核心的代码,配置就上面的俩。其他的都是相当简单的东西。

40f5436f9c35e1467c963010ec12e044.png

完整的代码工程

同步发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值