Logging with OSGi and knopflerfish
In the previous post Getting started We had prepared our Target platform to run on Equinox and tested it, also we noticed that the logging is not working so today we will configure it on both Equinox and Knopflerfish which is my favourite as it has a very easy to use GUI, which enables developers to easily work with it …
Now log4j.properties is a resources file like any other resources, the thing is in an OSGi environment you can not inject resources unless you used a fragment to do so. a Fragment is an empty project that has it is own META-INF directory that contains a MANIFEST.MF file to configure the fragment. now we have two options to make a fragment either we do it without a src folder just resources directly or we do make an src folder and put resources in it, now some would say that we don’t need the src folder as we are not going to write any code in the fragment, while this is true you need to know the fact that adding a fragment without src folder will work as expected on Equinox but it wont on knopflerfish, on the other hand making the fragment with src folder that contains the resources will be picked in the expected way by both Equinox and knopflerfish(please note that am not working on felix not yet, but I’ll update this info when I test it).
to sum it up we are going to make a fragment that will deliver resources data for a bundle(in our case it is a log4j.properties to org.apache.log4j), and we are going to make sure that this bundle will be working correctly on both Equinox and Knopflerfish. So lets get started!
- We will make a Fragment project, go to:
File –> New –> Other –> Plug-in Development –> Fragment Project
I will call it Log4jConfig , keep the create a java project checked as deiscussed erlier and change the Target Platform to an OSGi framework and choose Equinox!
now we will configure our fragment as shown in the following pic! and note that we can only add one host for each fragment, I have chosen “org.springframework.osgi.log4j.osgi” now click finish.
Now in the src folder make a new file and call it log4j.properties, I have configured one log4j. your workspace should looks like this:
If you take a look at our target folder you will see that we have two bundles provides log4j “org.springframework.osgi.log4j.osgi” and “com.springsource.org.apache.log4j” and both of them exports the “org.apache.log4j” which will make a conflict when trying to run the project! to solve this we need to deactivate the second one as our fragment is already hosted by “org.springframework.osgi.log4j.osgi”, to do that go to run configurations and un select the the “com.springsource.org.apache.log4j” from the target platform.
click run and you should see logging on your console!
now we need to test on knopflerfish:
- To deploy the log4jConfig fragment right click on it and click Export:
plug-in Development –> Deployable plug-ins and fragments
and click next and in the directory choose the OSGi-Target project root. and click finish.
Refresh you OSGi-Target and you will find a new folder called plugins containing a JAR file called log4jConfig_1.0.0.jar
now open Knopflerfish and follow the instructions:
- add the log4jConfig_1.0.0.jar to it by
File –> Open bundle from file
- make sure that it is status is installed not resolved. (if it is resolved click on the refresh button).
- as in first step add the following bundles from our target folder:
- start them all and make sure to leave the “spring-osgi-extender-1.1.0.jar” to the end (last one to start). you should see the logging on the Knopflerfish console:
- If you want to turn off Knopflerfish make sure to stop “spring-osgi-extender-1.1.0.jar” as if you don’t when you start Knopflerfish next time logging wont work! because the status of the log4jConfig_1.0.0.jar will be resolved and not installed.