Log4J SocketAppender handles debug information from remote clients

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

      

with the following configuration:

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> 

      

And the client application:

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

      

Which is correctly logged in the file 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> 

      

Excerpt from log file:

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

      

My question is, how do I make the final line the [main] (?:?) - Testing!

record of the node that sent it, as well as the line it happened on?

+3


source to share


1 answer


Add to

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

      

in the "async" appender too.



And try with this Client class

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

      

+4


source







All Articles