OSGi Logging Refork

EDIT 2015-08-27: Code has moved to https://github.com/dmarell/slf4j-osgi, https://github.com/dmarell/logback-osgi.

This article is about two libraries I made available today:

slf4j-osgi is a bridge between the logging facade slf4j and the OSGi LogService.

logback-osgi is an OSGi logging backend implemented with Logback.

Both are forks of Nogunner’s osgi-logging


Some time ago I, when I was setting up an OSGi development environment, I stumbled over Nogunner’s articles about OSGi Logging. The libraries did exactly what I wanted so I just installed the artifacts as they were into my maven repo and continued living my Slf4j-enriched OSGi-life.

These libraries/bundles solved a significant problem: Allowing code using Slf4j to fit nicely into an OSGi framework. The problem is that if you are a good OSGi-citizen you are expected to log using the OSGi LogService and not Slf4j, Log4j, Commons Logging, etc.

Recently, someone changed something i Slf4j forcing me to upgrade from Slf4j 1.5 to 1.6.x. I needed the source.

The code was initially made available by Nogunner at http://code.google.com/p/osgi-logging but development has stopped there. After that someone created a fork, but that fork is pointing to a dead host 🙁

So I did a new fork. I am sorry that I am continuing fragmenting the world, but I hope that at least someone more than myself can make use of this fork.

One of the first things I did was to mavenize them (=convert from Ant to Maven), along with many other small adjustments, including moving up to Slf4j to 1.6.4 and doing minor code inspection fixes.

The library slf4j-osgi provides a bridge from Slf4j to LogService. In order to use it, add a maven dependency to it, add some package statements to the maven-bundle-plugin configuration and finally in your code add a call to initialize it. Now all calls to the Slf4j api inside this bundle gets redirected to the OSGi LogService.

If you want to add a Logback logging backend, the bundle logback-osgi implements that. Note that this bundle is unrelated to the slf4j bridge and that it make perfectly sense to use it alone. In order to use it, install the bundle and add a system property pointing out your logback.xml.

Using the Libraries

In order to use the slf4j bridge, add a call in your code to initialize it:

public class TestComponent {
  protected final Logger log = LoggerFactory.getLogger(this.getClass());

  protected void activate(ComponentContext context) {
    log.info("activate TestComponent");

  protected void deactivate(ComponentContext context) {
    log.info("deactivate TestComponent");

Add the maven dependency:


and the Maven repo:


finally add the following to the configuration of maven-bundle-plugin:


Testing it

The SVN repo contains a full example of an OSGi bundle utilizing the slf4j-osgi module. In order to run it:

  1. Download and install Knopflerfish: http://www.knopflerfish.org/releases/3.2.0/knopflerfish_osgi_sdk_3.2.0.jar
  2. Checkout the code from https://marell.se/svn/public/slf4j-osgi-test
  3. Build it: mvn install
  4. Create a run target with main class org.knopflerfish.framework.Main with the root directory of this code as cwd, where the file init.xargs is located and add the following to VM options: -Dlogback.configurationFile=logback.xml
  5. Edit paths in the file init.xargs to match your Knopflerfish installation
  6. Run it. The KF GUI will appear and load the bundles listed in init.xargs. If it works, it will look soething like:
  7. Try to stop and start the module slf4j-osgi-test. If it works, standard output in your IDE will display the loggings as specified by the logback.xml in the same directory:
19:13:42.969 [BundleStart #11] DEBUG se.marell.slf4j-osgi-test - activate TestComponent,
19:13:42.969 [BundleStart #11] INFO  se.marell.slf4j-osgi-test - activate TestComponent

and the log tab in the Knopflerfish GUI will also show the loggings:

finally, you can observe that the file osgitest.log was created in cwd and that it contains the same loggings.

Now I hope that both you and I can continue our lifes just using OSGi and Slf4j logging without having to spend more time and effort on getting basic stuff like this working.

IT Consultant at CAG Edge. Cloud and Continuous Delivery specialist, software developer and architect, Node.js, Java.

Publicerad i Java, OSGi


WP to LinkedIn Auto Publish Powered By : XYZScripts.com