gpt4 book ai didi

java - 通过 LogEvent 对象提取通过 SocketAppender 发送的日志

转载 作者:行者123 更新时间:2023-12-02 11:50:13 25 4
gpt4 key购买 nike

这是我的配置文件或log4j2.properties文件

#Socket Appender
appender.socket.type=Socket
appender.socket.name=Socket_Appender
appender.socket.host=10.176.250.5
appender.socket.port=9000
appender.socket.layout.type=SerializedLayout
appender.socket.connectTimeoutMillis=2000
appender.socket.reconnectionDelayMillis=1000
appender.socket.protocol=TCP
#Loggers
logger.socket.name=atom
logger.socket.level=debug
logger.socket.appenderRefs=socket
logger.socket.appenderRef.socket.ref=Socket_Appender

接下来我有两个类 LogSender 和 LogListener

日志发送器

它的工作是通过 SocketAppender 发送日志。

What is sent over the Stream is an LogEvent Object.

public class LogSender {

public static final Logger LOG = LogManager.getLogger("atom");

public static void main(String[] args) {
LOG.trace("Error 1");
LOG.debug("Error 2");
LOG.info("Error 3");
LOG.warn("Error 4");
LOG.error("Error 5");
LOG.fatal("Error 6");
}
}

日志监听器

这应该接收通过流发送的 LogEvent 对象。LogListener 正在使用 ObjectInputStream 并保存

LogEvent event = null;
private Socket socket1 = null;
private ServerSocket ss = null;
PrintWriter sspw=null;
ObjectInputStream objectInputStream = null;
try {
//Creating Socket and making it listen to a specific port, receiving through streams.
//Servers socket creation and waiting
ss = new ServerSocket(9000);
socket1 = ss.accept();

objectInputStream = new ObjectInputStream(new BufferedInputStream(socket1.getInputStream()));

event = (LogEvent) objectInputStream.readObject();
System.out.println(event.getLoggerName());
System.out.println(event.getLevel());
System.out.println(event.getMessage().getFormattedMessage());
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Output

atom

DEBUG

Error 2

输出是预期的记录器名称。这意味着我们正在接收 LogEvent 对象并能够提取记录器信息。我还能够接收实际的日志消息,但仅弹出其中一个级别,即调试级别。

我也想要所有其他级别。我将如何继续做到这一点?

我对 Log4j2 很陌生,我觉得我在理解上错过了一些东西(只是直觉)。

最佳答案

您没有获得第一个日志 (TRACE),因为在您的配置文件 log4j2.properties 中,您有 logger.socket.level=debug,但您应该还可以使用 logger.socket.level=trace 启用级别 TRACE。

关于其他日志,您应该循环运行监听器代码:现在您只读取第一个日志。

我没有测试这段代码,但它应该看起来像:

LogEvent event = null;
private Socket socket1 = null;
private ServerSocket ss = null;
PrintWriter sspw=null;
ObjectInputStream objectInputStream = null;
try {
//Creating Socket and making it listen to a specific port, receiving through streams.
//Servers socket creation and waiting
ss = new ServerSocket(9000);
socket1 = ss.accept();

objectInputStream = new ObjectInputStream(new BufferedInputStream(socket1.getInputStream()));

while (true) {
event = (LogEvent) objectInputStream.readObject();
System.out.println(event.getLoggerName());
System.out.println(event.getLevel());
System.out.println(event.getMessage().getFormattedMessage());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (objectInputStream != null) {
objectInputStream.close();
}
}

关于java - 通过 LogEvent 对象提取通过 SocketAppender 发送的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com