gpt4 book ai didi

java - Log4J SocketAppender 吞下来自远程客户端的调试信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:54 24 4
gpt4 key购买 nike

我配置了一个 SimpleSocketServer

public class Simple {

public static void main(String[] args) {
try
{
String[] arguments = {"4445", "src/server.xml"};
SimpleSocketServer.main(arguments);

}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}

具有以下配置:

服务器.xml

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p (%d{DATE}) [%t] (%F:%L) - %m%n"/>
</layout>
<param name="File" value="C:/ponies/async.log"/>
<param name="MaxFileSize" value="10KB"/>
<param name="MaxBackupIndex" value="5"/>
</appender>

<root>
<param name="level" value="INFO"/>
<appender-ref ref="rolling" />
</root>
</log4j:configuration>

和一个客户端应用程序:

public class Client {

private static Log logger = LogFactory.getLog(Client.class);
public static void main(String[] a) {
// -- Start a new SocketNode

try {
DOMConfigurator.configure("client.xml");
String m_hostName = "localhost";
String m_port = "4445";
System.out.println("Accessing socket on host " + m_hostName
+ " through port " + m_port);
Socket socket = new Socket(m_hostName,
new Integer(m_port).intValue());

// -- All right so far, now create the SocketNode
System.out.println("Create the SocketNode listener");
SocketAppender sa = new SocketAppender("localhost", 4445);
System.out.println("Activating!");
sa.activateOptions();
System.out.println("Attempting to log");
String logMsg = "Writing log event through SocketAppender instance";
LoggingEvent le = new LoggingEvent("category",Logger.getRootLogger(),new Date().getTime(),Level.DEBUG,logMsg,new Throwable());
System.out.println("Appending!");
sa.append(le);
logger.info("Testing!");
new SocketNode(socket, new Hierarchy(Logger.getRootLogger()));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

正确记录到文件 async.log

客户端.xml

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  
<appender name="async" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="console" />
<appender-ref ref="socket" />
<param name="Blocking" value="false"/>
<param name="bufferSize" value="256"/>
</appender>

<appender name="socket" class="org.apache.log4j.net.SocketAppender">
<param name="Port" value="4445"/>
<param name="RemoteHost" value="localhost"/>
<param name="ReconnectionDelay" value="5000"/>
<param name="LocationInfo" value="true"/>
</appender>

<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>

<logger name="com.test.Client">
<param name="level" value="INFO"/>
</logger>

<root>
<param name="level" value="INFO"/>
<appender-ref ref="async" />
</root>

</log4j:configuration>

日志文件摘录:

INFO (30 Mar 2012 09:53:21,592) [main] (SimpleSocketServer.java:63) - Waiting to accept a new client.
INFO (30 Mar 2012 09:53:24,466) [main] (SimpleSocketServer.java:65) - Connected to client at /127.0.0.1
INFO (30 Mar 2012 09:53:24,466) [main] (SimpleSocketServer.java:66) - Starting new socket node.
INFO (30 Mar 2012 09:53:24,466) [main] (SimpleSocketServer.java:63) - Waiting to accept a new client.
INFO (30 Mar 2012 09:53:24,466) [main] (SimpleSocketServer.java:65) - Connected to client at /127.0.0.1
INFO (30 Mar 2012 09:53:24,466) [main] (SimpleSocketServer.java:66) - Starting new socket node.
INFO (30 Mar 2012 09:53:24,498) [main] (?:?) - Testing! //This is the line of interest

我的问题是如何使最后一行 [main] (?:?) - Testing! 记录发送它的节点以及它出现的行?

最佳答案

添加

<param name="LocationInfo" value="true" />

也在“异步”appender 上。

然后尝试使用这个 Client 类

public static void main(String[] a) {
try {
DOMConfigurator.configure("src/forum9944681/client.xml");
logger.info("Testing!");
Thread.sleep(60000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

关于java - Log4J SocketAppender 吞下来自远程客户端的调试信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9944681/

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