Log4J SocketAppender吞下远程客户端的调试信息

时间:2012-03-30 14:14:16

标签: java sockets log4j cluster-computing

我已配置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());
        }  
}
}

使用以下配置:

server.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

client.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!记录发送它的节点以及它发生的行?

1 个答案:

答案 0 :(得分:4)

添加

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

on“async”appender。

尝试使用此客户端类

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());
    }
}