Apache log4j as log server

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.

About nilshb

Hi! My name is Nils Harald Berge. I am an plain old programmer who occasionally document stuff in my blog.
This entry was posted in Java. Bookmark the permalink.

4 Responses to Apache log4j as log server

  1. inge johnsen says:

    very cool.

    have a look at the ndc (nested diagnostic context) in log4j as well. we use this in a servletfilter where we push/pop the userdata, forcing all subsequent log entries to output these userdata in the log (in front of the actual log message, after the dates/time etc.). very neat way to log userspecific entries without retreiving this data in your application code 🙂

  2. André says:

    I’m skeptical. RPC tends to fail in interesting ways (http://research.sun.com/techrep/1994/abstract-29.html). If you do this, your code has to be aware on some level of which log statements are remote, so it can recover from failure.

    The SocketAppender docs state “… However, if the network connection is slower then the rate of event production, then the client can only progress at the network rate. In particular, if the network link to the the server is down, the client will be blocked.”

    IOW, a servlet-based web app will quickly run out of request processing threads if the network link to the logger goes down, unless the app aborts long-running calls to networklogger.foo() automatically.

  3. Amir says:

    Try AsyncAppender to avoid progressing only at the network rate when using SocketAppender.
    Note that the AsyncAppender can only be script configured using the org.apache.log4j.xml.DOMConfigurator class.

  4. Archibald says:

    Hi,
    Suggest following to achieve performance, availability, traceability and usability:
    on clustering / load balancing; log to filesystem on each node. Then consolidate them with separate logreader app on each node that send them to a centralized log.

Leave a comment