gpt4 book ai didi

java - 无法在Log4j2中实现TcpSocketServer

转载 作者:行者123 更新时间:2023-12-01 06:01:45 28 4
gpt4 key购买 nike

我正在尝试使用 SocketAppender 将 SpringBoot 应用程序中的日志写入远程服务器在 Log4j2 中。首先,我运行监听器应用程序(这也是一个 Spring Boot 应用程序),我在其中实现了 TcpSocketServer然后我运行我的主应用程序。我收到以下错误。主应用程序在 localhost:8080 上运行,监听器应用程序在 localhost:9600 上运行。

我的做法正确吗?我怎样才能正确地实现这个日志记录?我在网上找不到合适的例子。

In listener application

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens

In main application

2019-06-20 17:24:28,091 main ERROR Unable to write to stream 
TCP:localhost:9600 for appender socket-appender: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:localhost:9600
2019-06-20 17:24:28,092 main ERROR An exception occurred processing Appender socket-appender org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:localhost:9600
Caused by: java.net.SocketException: Broken pipe (Write failed)

我的实现

Log4j2 configurations in main app

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Appenders>
<Socket name="socket-appender" host="localhost" port="9600">
<JsonLayout properties="true"/>
</Socket>
</Appenders>
<Loggers>
<AsyncLogger name="socket-appender" level="info">
<AppenderRef ref="socket-appender"/>
</AsyncLogger>
<AsyncRoot level="info">
<AppenderRef ref="socket-appender"/>
</AsyncRoot>
</Loggers>
</Configuration>

Log4j2 configurations in listener app

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="SocketServer">
<Appenders>
<RollingRandomAccessFile name="Rolling-Random-Access-File-Appender"
fileName="logs/rollingrandomaccessfile.log"
filePattern="archive/logs/rollingrandomaccessfile.log.%d{yyyy-MM-dd-hh-mm}.gz">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger>
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncLogger>
<AsyncRoot level="info">
<AppenderRef ref="Rolling-Random-Access-File-Appender"/>
</AsyncRoot>
</Loggers>
</Configuration>

TcpSocketServer Implementation in listener app

package com.springReceiver.receiveFromSocket;

import org.apache.logging.log4j.core.net.server.ObjectInputStreamLogEventBridge;
import org.apache.logging.log4j.core.net.server.TcpSocketServer;

import java.io.IOException;
import java.io.ObjectInputStream;

class Receiver {

void writeLogs(){

TcpSocketServer<ObjectInputStream> server = null;
try {
server = new TcpSocketServer<>(9600,new ObjectInputStreamLogEventBridge());
server.run();
}
catch (IOException e)
{
e.printStackTrace();
}


}
}

最佳答案

在客户端尝试这个配置:

<Socket name="socket" host="xxxxxxxxx" port="1090" connectTimeoutMillis="1000" reconnectionDelayMillis="1000" >
<SerializedLayout />
</Socket>

关于java - 无法在Log4j2中实现TcpSocketServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680479/

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