imesh
imesh

Reputation: 123

How to use log4j with udp

Can anyone tell how to use log4j for UDP packet? I need my program to use log4j so that it pass the data to a listener that retrieve UDP packets.

Thanks in advance

Upvotes: 3

Views: 3018

Answers (2)

imesh
imesh

Reputation: 123

I was able to integrate log4j successfully with UDP server. Here what I did. I used log4j2:

The two jar files are

  • log4j-core-2.0-beta4.jar
  • log4j-api-2.0-beta4.jar

Client sends with logger.debug("Test message");.

And the server receives it as below:

import org.apache.logging.log4j.core.LogEvent;

LogEvent logEvent = null;
ObjectInputStream obj = null;

enter code here
bis = new ByteArrayInputStream(UDPpacket.getData());
obj = new ObjectInputStream(bis);
logEvent = (LogEvent) obj.readObject();

System.out.println("Got it : " + logEvent.toString());
System.out.println("Got Message : "+ logEvent.getMessage().getFormattedMessage());

Upvotes: 2

Tormod Hystad
Tormod Hystad

Reputation: 2405

To get logging via UDP to work for Log4j 1.2 we have downloaded the source for a UDPAppender from the now abandoned(?) "Apache Receivers Companion for log4j 1.2" project (for backporting usable code from the abandoned Log4j 1.3 project) and compiled it ourselves. Initial tests indicates it works fine, BUT hopefully you noticed that I used "abandoned" twice above...

Project page: http://logging.apache.org/log4j/companions/receivers/index.html

Source code (NOT in the repo linked from the project page, they have moved it to "extras"): http://svn.apache.org/viewvc/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/receivers/net/

Upvotes: 2

Related Questions