Chapter 2. Logging and Tracing with Eclipse PDE and Log4J


2.1. Logging and Tracing with Log4J

5.7. Diagnosing problems section of Spring-DM reference manual gives you a good overview of logging and tracing strategies used by Spring-DM.

This section will show you how to configure Log4J to be used with Spring-DM.

We want to do this step before we start developing the Spring-DM projects as it will help us to diagnose potential problems.

In the previous step when we started Target Platform we’ve seen a warning message telling us that our Log4J configuration is missing. And indeed it is. Although Log4J bundle is deployed successfully it is missing configuration.

In order to provide Log4J configuration we are going to create and OSGi™ Fragment bundle, a technique widely used to attach extra configuration to already deployed bundles. In other words we will create an OSGi™ Fragment Bundle which will contain file and will attach it to Log4j bundle. To do that we will utilize Eclipse PDE which allows us to not only create OSGi™ bundles, but also deploy them without packaging them into JARs, which is very useful and convenient for configuration bundles such as Log4J, which often changes during the development.

Let's create a new Project that will represent such Fragment Bundle

Click on: File -> New -> Project

Expend: Plug-in Development and select Fragment Project

Click Next

Enter the following in the corresponding fields:

Project name:org.springframework.osgi.log4j.config

Uncheck: Create a Java Project

Check: OSGi framework (Equinox)

Click Next

Enter Log4J Configuration as Fragment Name:

Click on Browse (next to the Host bundle) and select bundle. This is the bundle to which we will attach this fragment.

Click OK -> Finish


Also, uncheck bundle, since we already have another Log4j bundle and this one was pulled as a dependency by SLF4J bundles during Maven downlad and copy.

At the root of the newly created project create file with the following contents:

log4j.rootLogger=info, A
log4j.appender.A.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Later on you can modify it as you wish

Your newly created Fragment Bundle project should look like this:

Note, how Eclipse already pre-created an OSGi structure for us with META-INF directory and MANIFEST.MF file. It also made it available to our Target Platform without any special deployment procedure. Open Target Platform and you should see our Log4J fragment bundle there:

Now we can start the Target Platform again. Note, we no longer see the warning log4j message, but rather INFO messages coming out of Tomcat bundle which shows us that the server was started and is available on Port 8080.

. . . . . . . . . . . . . . .
891 [Tomcat Catalina Start Thread] INFO org.apache.catalina.core.StandardService
				 - Starting service Catalina
891 [Tomcat Catalina Start Thread] INFO org.apache.catalina.core.StandardEngine
				 - Starting Servlet Engine: Apache Tomcat/5.5.23
906 [Tomcat Catalina Start Thread] INFO org.apache.catalina.core.StandardHost
				 - XML validation disabled
1110 [Tomcat Catalina Start Thread] INFO org.apache.coyote.http11.Http11BaseProtocol
				 - Starting Coyote HTTP/1.1 on http-8080
. . . . . . . . . . . . . . .

2.2. Eclipse PDE Tracing

Our bundles will be deployed in Eclipse Equinox which is an OSGi™ platform which Eclipse is based on and which is provided by Eclipse PDE environment.

One of the features that you can use to debug and diagnose potential problems is Tracing facility provided by Eclipse PDE

Open up Spring-DM-TP launch configuration.

Click on: Tracing tab

Make sure Tracing is enabled and debug/service is checked

Click on: Apply -> Run

You should see lots of trace messages. We will see how they can help us when we develop and deploy our first project

Now we're ready to continue. . .