Configuring log4j with XML in OSGi

 Comments

Sometime back, I had to make perf4j work on OSGi. It required log4j as the logging library and purely demanded log4j.xml (it would not take the .properties file since the configuration must attach downstream appenders). I assumed that it should be simple, but unfortunately it was not. In the rest of this post, I describe how I could get around this, and used log4j.xml to configure log4j logging in OSGi.

Some background on logging for OSGi

Logging was something that was looked as one of the earliest thing for building up our OSGi infrastructure libraries. Since most of the bundles and libraries require logging to be there, we were forced to provide its implementation at the early stages itself. What came as the first viable option was to use Pax Logging. It is still the best logging mechanism for OSGi. It can take configurations in a variety of ways, the most pleasing of all the available configuration models was using the ConfigAdmin service that would leverage the OSGi's dynamism. This is again the best was to configure any service, and not just logging. Even without any explicit configuration Pax Logging would just work fine with its default behavior with a DEBUG for all the bundles in the system.

We chose Pax Logging with ConfigAdmin service. At first the ConfigAdmin provider was chosen as Pax ConfMan. But due to its limitation of not providing runtime updates, I chose to opt for Pax Coin which is still under incubation, but has some good features including runtime property updates and capability to consume configuration data from multiple sources. I was happy to make Pax Coin consume configuration from file system. But I could find that it simply did not accept configurations from the xml file.

So, as of now though providing configuration data via ConfigAdmin seems to be a good choice, I could not find any quick solution to this. So I found a temporary hack for this.

Hack

Note: The following instructions are for working on Pax Runner. If you are not already using it, I would highly recommend it as a tool to start your OSGi framework. It would be the most elegant way to get started with OSGi.
  1. Remove any references to pax logging from your provisioning, so that it doesn't appear in your OSGi platform.
  2. Add commons-logging and log4j jars to the platform as bundles. Add the following URLs in your profile/provisioning file:
        wrap:mvn:commons-logging/commons-logging/1.1.1
        wrap:mvn:log4j/log4j/1.2.15
    
  3. Point to the log4j.xml file it OSGi classpath via Pax Runner. Its easy to append --cp=<path_to_dir> as a parameter when starting Pax Runner. The directory path_to_dir will now be in classpath, so you can place our log4j.xml there to be picked up by the log4j system.

Future

This was a temporary hack, but the concern is that I need to find a better way to make ConfigAdmin service consume XML.

OSGI OPS4J TIPS
blog comments powered by Disqus