[osgi logging] part 3 – slf4j + logback as osgi bundles

This is part 3 of my blogseries about ‘Logging in OSGI Enterprise Applications‘ – you’ll find all parts here.

Download SLF4J and Logback

Now its time to integrate SLF4J and Logback into an OSGI app – at first you should download the newest releases:

Search for Bundles

If we take a look at the MANIFEST.MF files from the different Jar’s we’ll see that only some include OSGI informations, so as good citizens of OSGI world we search if bundles are available from a repository:

At SpringSource (S2) we find Bundles for SLF4J (Version 1.5.6, where 1.5.8 is the newest) and Logback (Version 0.9.15, where 0.9.17 is the newest):

S2 SLF4J Bundles Version 1.5.6S2 Logback Version 0.9.15

Its a good idea to download the bundles from S2 even if you want to deploy your own with the newest releases. Its always good to take a look at the Dependencies and compare if you’re in trouble 😉

Which Bundles / Jar’s you need depends from your projects. For the use-cases described in the first parts of this series we’ll need:

  • SLF4J API – all our bundles logging to SLF4J / Logback only need a dependency to SLF4J API
  • Logback Classic + Logback Core – the Logback Implementation of SLF4J
  • JUL to SLF4J – if you have to integrate bundles using JDK Logging (java.util Logging)
  • Log4J over SLF4J – if you have to integrate bundles with Log4J Logging
  • Jakarta Commons Logging (JCL) over SLF4J – if you have to integrate Apache Commons Logging

SLF4J + Logback Jar’s as OSGI Bundles

As I wrote above, only some of the downloaded Jar’s from SLF4J and Logback include valid OSGI information:

  • You can use as OSGI Bundles without modifications:
    • slf4j-api-1.5.8.jar
    • jcl-over-slf4j-1.5.8.jar
    • logback-core-0.9.17.jar
  • Jar’s with wrong OSGI Information:
    • logback-classic-0.9.17.jar Exports slf4j.impl in version 1.5.6, but slf4j-api-1.5.8 imports 1.5.8, so I re-deployed this bundle
  • Jar’s without any OSGI Information: (so I deployed them new with added OSGI dependencies)
    • jul-to-slf4j-1.5.8.jar
    • log4j-over-slf4j-1.5.8.jar

In my other blogseries I already talked about HowTo use PDE to deploy a bundle (Plug-in) from an existing Jarfile, so I don’t want to repeat it here.

PDE and Cycles

Important: there are cycles between SLF4J API and Implementation (Logback):

SLF4J cycles between API and Implementation

The dependency between slf4j.api and logback.classic has arrows in both directions, this indicates that there’s a cycle.

BTW: if you’re wondering about the dependency graph – its from ‘PDE Graph Plug-in Dependencies View‘ I talked about here and here.

Because of these cycles you have to check “allow for binary cycles” if deploying the Plug-in using PDE:

PDE Export deployable Plug-Ins - allow cycles

PDE 2nd Dependency

Here’s another tip: if working with logging – bundles then it could happen that your IDE (or current Target Platform) contains more then one bundle providing the same packages. If you want to force PDE to use a specific bundle to resolve the dependency, simply open MANIFEST.MF and add the bundle to “Automatic Management of Dependencies“:

PDE AMD ui

example: you’re using Import-Package org.apache.commons.logging. Adding the bundle ‘jcl.over.slf4j‘ (Jakarta Commons Logging over SLF4J) to ‘Automated Management of Dependencies‘ you can use your Import-Package and on the other side be sure that PDE puts the jcl.over.slf4j bundle on its Classpath. Those ‘2nd Dependencies’ are not added to the MANIFEST.MF – you’ll find them in the built.properties:

PDE AMD buildproperties

At the end of this blog series I’ll provide download links, where you can get all needed bundles, so you don’t have to do it by yourself 🙂

you’ll find the overview of this blog series here. stay tuned… part4 follows…

16 responses

  1. Hallo,
    Ich habe heute versucht anhand ihres Blog das vorgeschlagene Logging nachzubauen leider scheitere ich unter Eclipse 3.5.1 schon damit das ich die hier in Teil 3 beschriebenen 6 Bundles zu bauen.
    Es scheitert an dembeschriebenen Cycle.
    Wie beschrieben habe ich den Hacken gesetzt und bei den Compiler optionen auch noch den “abbruch des build” ausgeschalten. Eclipse steigt beim Export aber leider trotzdem aus.
    Haben Sie ggf einen Tip wie ich dies umgehen/lösen kann?

    Mfg
    Heiko Ahlig

  2. Yes, it seems that it was built but than I get the following error:
    A cycle was detected when generating the classpath slf4j_api_1.5.8, logback_classic_0.9.17, slf4j_api_1.5.8.

    complete EventDetail: http://eclipseexamples.nacorswelt.de/eventDetails.jpg

    My build options you can see here:

    Did you have an other tip for the export?

    All other things of your blog I try today works fine.

    Greetings
    Heiko

    • whats your preferences… Java -> Compiler -> Building -> Build Path Problems: Circular Dependencies ?
      should be Warning (not Error)
      …next days I have to build all my bundles again with newest releases of SLF4J, Logback, Lilith
      if there are problems I’ll report – and if not I’ll report in detail again

      ekke

    • Did you try to deploy _all_ bundles by yourself? I had the same problem when I tried that, and setting Build Path Problems: Circular Dependencies to “Warning” did not help.

      Just using the bundles slf4j-api-1.5.8.jar, jcl-over-slf4j-1.5.8.jar, and logback-core-0.9.17.jar as the blog suggested (without modification) and only re-bundling the rest worked for me.

      By the way: Very helpful blog, ekke, thank you for all the work!

      Katja

  3. Hi,

    added all bundles. Logging works for my own classes and the spring. But it seems that the eclipse logs are not collected. Added in my logback configuration. Root logger has level INFO. Any idea?

    • Hello,
      I’ve a lot of trouble integrating your example in my rcp application.
      I always got “java.lang.ClassNotFoundException: org.slf4j.spi.LoggerFactoryBinder”

      I tried to build all my external-jar-plugins on my own, because I don’t want to fix external deliveries with my own fixes.
      We want to use the application for a sensible area where I have to guarantee the content of the jars and their sources.

      Another trouble, but this is a general problem, is the integration of external bundles.
      Bundles which are available through the market are easy to integrate, but every colleagues has to import them to on his own.
      Bundles which are only available through direct download (like SpringSource) have to be put into something like a target.
      Bundles which are incomplete and I have to create on my own, are placed in my Eclipse Project.

      It is awful to include 3rd party bundles and the worst thing, it is not running out-of-the-box, because users have to install them probably from different sources into different places with a lot of different kind of configuration.

      In my opinion the “target-concept” should be replaced by putting all external jars directly into the workspace, so that they will be shown in the same hierarchy as my own bundle projects. Configuring the target over preferences makes it always to a part of the IDE not of the project.
      In this case it is easy to get an overview over bundle conflicts, which is probably the problem in this case (I can see my own external-slf4j-bundle and 2 different versions of slf4j in my ide bundle repository, but they are not marked in the run-configuration!)

      Sorry, I’m a little bit upset, trying to implement _your_really_good_ idea since 2 days…

      regards Oliver

Leave a comment