Log4j has been the de facto logging standard for Java applications a long time. And with good reason. It’s flexible, easy to use, jet extremely powerful. But, as many other open source projects, it lacks good (free) documentation. This is understandable since the main author, Ceki Gülcü, has made business of selling the documentation.

One of the secrets buried in the log4j.jar is its socket server. This is a simple class with a main method. It takes two parameters, port and configuration file. Both work just as you would expect. The config file can be specified both in XML or as properties.

Example server startup:

org.apache.log4j.net.SimpleSocketServer 4712 log4j-server.properties

Now all you have to do is spesify your appender on the client.

Example appender:

log4j.appender.SERVER=org.apache.log4j.net.SocketAppender
log4j.appender.SERVER.Port=4712
log4j.appender.SERVER.RemoteHost=loghost
log4j.appender.SERVER.ReconnectionDelay=10000

I use this in applications where i have load balancing between multiple hosts. Logs scattered all over the place is a mess. This way you can keep them all in one place. There is one problem with this, if the server is dead all your logging disappears in a black hole. For this reason i also log important stuff to the local filesystem, to serve as backup.

Advertisement