Hide
Siafoo – the intersection of pastebin, help desk, version control, and social networking Join Siafoo Now or Learn More

Log4j XMLSocketReceiver example Atom Feed 0

In Brief This is the most basic setup for receiving log4j events from an XML Socket. When run the application will listen on por 4448 and print logging events to standard out. For an example of configuring a Pylons application to send events to an xml socket see Lumberjacking with log4j's Chainsaw... more
# 's
 1import org.apache.log4j.Hierarchy;
2import org.apache.log4j.LoggerRepositoryExImpl;
3import org.apache.log4j.WriterAppender;
4import org.apache.log4j.Level;
5import org.apache.log4j.TTCCLayout;
6import org.apache.log4j.net.XMLSocketReceiver;
7import org.apache.log4j.spi.LoggerRepository;
8import org.apache.log4j.spi.RootLogger;
9
10public class LogTest{
11
12 public static void main(String[] args) throws InterruptedException, java.io.IOException {
13
14 LoggerRepository lrRead = new LoggerRepositoryExImpl(new Hierarchy(new RootLogger(Level.DEBUG)));
15
16 WriterAppender handler = new WriterAppender(new TTCCLayout(), System.out);
17 lrRead.getRootLogger().addAppender(handler);
18
19 XMLSocketReceiver receiver = new XMLSocketReceiver(4448, lrRead);
20
21 receiver.activateOptions();
22 // Wait for keyboard input (a silly way of blocking the current thread)
23 System.in.read();
24 }
25}

This is the most basic setup for receiving log4j events from an XML Socket. When run the application will listen on por 4448 and print logging events to standard out. For an example of configuring a Pylons application to send events to an xml socket see Lumberjacking with log4j's Chainsaw

To compile it use a command like javac LogTest.java -cp $LOG4JLIB/log4j-1.2.15.jar:$LOG4JLIB/apache-log4j-receivers-1.0.jar:$LOG4JLIB/apache-log4j-component-1.0.jar

To run it use a command like java -cp $LOG4JLIB/log4j-1.2.15.jar:$LOG4JLIB/apache-log4j-receivers-1.0.jar:$LOG4JLIB/apache-log4j-component-1.0.jar:. LogTest

You would need to either use the abandoned Log4j 1.3 branch or the Log4j 1.2 branch with the receivers, and component companions