Getting SocketNode and SocketAppender to work
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, November 24, 2005 11:02 AM

For the past week I've been struggeling hard to get remote logging working using the log4j SockerNode and SocketAppender, but failed to succeed. Unfortunately there is not so much information in the log4j.net package to be found on the internet, lets forget about a working example This is what I've been doing so far: SimpleSocketServer On the server I'm running a SimpleSocketServer instance on port 4445 through the command: java -classpath D:RemoteLog4JServerliblog4j-1.2.12.jar org.apache.log4j.net.SimpleSocketServer 4445 simplesocketserver.properties The simplesocketserver.properties file looks like: log4j.rootLogger=debug,stdout,R    More>>

For the past week I've been struggeling hard to get remote logging working using the log4j SockerNode and SocketAppender, but failed to succeed. Unfortunately there is not so much information in the log4j.net package to be found on the internet, lets forget about a working example



This is what I've been doing so far:



SimpleSocketServer

On the server I'm running a SimpleSocketServer instance on port 4445 through the command:

java -classpath D:RemoteLog4JServerliblog4j-1.2.12.jar org.apache.log4j.net.SimpleSocketServer 4445 simplesocketserver.properties



The simplesocketserver.properties file looks like:




log4j.rootLogger=debug,stdout,R



log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout



# Pattern to output the caller's file name and line number.

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n



log4j.appender.R=org.apache.log4j.RollingFileAppender

log4j.appender.R.File=remote_log.log



log4j.appender.R.MaxFileSize=1024KB



log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n




On the system console I see this server is running fine.



Next step is to start a SocketNode-instance through a Java application. The main code of this class looks like:




//-- Start a new SocketNode

try {

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");

new SocketNode(socket, new Hierarchy(Logger.getRootLogger()));

}

catch .....




Host and port are passed through application parameters and are 127.0.0.1 and 4445



Again on the server console it says the listener is bound to the socket on port 4445 ...



The last part of the puzzle is a test client application which sends remote logging events to the server.

This body of this class looks like:




static Logger logger = Logger.getRootLogger();



public static void main(String argv[]) {

// Try sending logging events through the SocketAppender in the log4j_remote.properties file

System.out.println("Try sending logs through the SocketAppender in the log4j.properties file");

PropertyConfigurator.configure("log4j_remote.properties"); //Logs to a SocketAppender; host 127.0.0.1, port 4445

logger.debug("Hello world");

logger.info("What a beatiful day.");



// And try sending logging events through SocketAppender instance

System.out.println("Try sending logs through a SocketAppender instance");

SocketAppender sa = new SocketAppender("171.21.241.122", 4445);

System.out.println("Created SocketAppender instance");

String logMsg = "Writing log event through SocketAppender instance";

LoggingEvent le = new LoggingEvent("fox.teststuff.TestLog4J",Logger.getRootLogger(),Level.DEBUG,logMsg, new Throwable());

sa.append(le);

sa.close();

}




As you can see I tried remote logging on two ways:

1.

Through the log4j_remote.properties file by configuring a SocketAppender. This file looks like:




log4j.rootCategory=DEBUG,stdout,A1



# ConsoleAppender

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout



# Pattern to output the caller's file name and line number.

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n



# A1 is set to be a SocketAppender sending its output to the server running on the remote host, port 12345.

log4j.appender.A1=org.apache.log4j.net.SocketAppender

log4j.appender.A1.Port=4445

log4j.appender.A1.RemoteHost=[IP_ADDRESS_SERVER]




2.

By using a SocketAppender instance.



But neither work. The log remote_log gets created as the SimpleSocketServer is started. It contains entries of the successfull starting of the server and the SocketNode.

I also see the logs coming in the client console on running the test applicattion.



There is no firewall between client and server, and I am able to setup a tellnet session from client to server and vice versa through port 4445 (after launching the SimpleSocketServer).

Furthermore I used some IP-sniffing tools to check the IP-traffic from client to server, and this shows me the TCP/IP packet is successfully sent on the client, and successfully received on the server.



But no client-entries appear in the server log :-(



I would be very gratefull if someone has got a clue. And you can be sure I will pass this knowledge on to the community.



Thanks in advance, kind greetings,

Harro de Vos

   <<Less

Re: Getting SocketNode and SocketAppender to work

Posted By:   Anonymous  
Posted On:   Friday, November 25, 2005 05:21 AM

UPDATE:

With a lot of thanks to my collegue Pattrick I get a lot closer to the solution.



I made some conceptual mistakes:



1.

The SimpleSocketServer is already using the SocketNode to create a log-listener on the specifief host and port.
It was therefore needless and wrong for me to start a seperate SocketNode instance through the StartSocketNode.
This probably only causes conflicts.



2.

A SocketNode-instance should be started within a Thread. Check out the code in Log4J's SimpleSocketServer



3.

In my testclass I tried to send remote log-events through configuring a SocketAppender in the log4j.properties file AND through a coded instance of the SocketAppender.

It might cause problems implementing both methods in the same thread.



By now I have both working! Only remaining issue is the fact I get SocketExceptions after the log-entries have been sent:




INFO Thread-0 org.apache.log4j.net.SocketNode - Caught java.net.SocketException closing conneciton.




Maybe someone knows a solution to this problem ... furthermore I hope other developers can make use of this info



Greetings, Harro de Vos
About | Sitemap | Contact