Archive for November, 2008

Eclipse Demo Camp right after Eclipse Summit

If you are interested in Spring DM, OSGi, dynamic servers with Eclipse or just in Vodka, then you might want to visit our Eclipse Demo Camp on November 21st in Ludwigsburg. This Demo Camp which we organize with our friends form itemis will be held in the fancy MTV Campus, very close to Eclipse Summit.

More information can be found in the Eclipse Wiki.

PS

Already registered for Eclipse Summit?

IP Address based Logging in JSF

This article describes how to add more significance to your Log4J log files in a JSF (or any Web) environment, by adding the client specific information to the log files. Even if you already have decent logging in your application, log files can be often very hard to read if you have several users connecting to your website at the same time. The (nearly) only unique attribute of a user is his/her IP address.

This article will describe how to add a client’s IP address to your JSF application’s logging in a very easy way. It is based on Log4J and uses the Message Diagnostic Context (MDC) to add client based logging information.

MDC

MDC is an instrument in Log4J to distinguish interleaving log output from different sources. Log output is typically interleaved when a server handles multiple clients simultaneously. We will make use of this mechanism to distinguish different clients according to their IP addresses.

Add IP address information to Log4J’s MDC

First we need to add the client’s IP address to the MDC. Therefore we read the client’s IP address in the Faces Servlet and store it to the MDC. Below you see the definition of the Servlet in web.xml and an excerpt from the Servlet Code.

web.xml


 Faces Servlet
 
     com.weiglewilczek.jsf.CustomFacesServlet
 
 -1

Servlet

public class CustomFacesServlet implements Servlet
{
   …
   public void service(ServletRequest request, ServletResponse response)
 throws IOException, ServletException
  {
      String remoteAddress = request.getRemoteAddr();
      MDC.put("remoteAddress", remoteAddress);
       m_Delegate.service(request, response);
  }
}

First the client’s IP address is read from the request with the method request.getRemoteAddr() and stored into a variable “remoteAddress”. This information is added to the MDC with the key “remoteAddress” by calling MDC.put(”remoteAddress”, remoteAddress). Note that all MDC methods are static.

Log4J configuration

Below you see the Log4j conversion pattern which includes our MDC information added above. The argument “%X{remoteAddress}” reads the value, identified by the key “remoteAddress” from the MDC.

Log4j.appender.CONSOLE.layout.conversionPattern=%d{ABSOLUTE}
%-5p [%c{1}] - %X{remoteAddress}: %m%n

Result

The Log now includes the IP Address:

2008-08-18 09:05:48,543 INFO [com.weiglewilczek.jsf….] – 127.0.0.1: Logging Output