7
votes

Here is my configuration:

We are developing an OSGi application and want to include logging. I decided to use slf4j + logback.

We are using Eclipse as an IDE and Tycho to benefit from the Eclipse IDE like Manifest Editor and so on.

So I have tried the following:

Created a new plugin with the following Manifest.mf:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Createcommand
Bundle-SymbolicName: de.hswt.oms.ws.wsr.createcommand
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: de.hswt.oms.ws.command.wsr,
 de.hswt.oms.ws.ds.core.data.impl,
org.slf4j
Service-Component: OSGI-INF/component.xml

Now when i move to Run -> Run Configurations -> OSGi Framework and select my Bundle and click "Add Required plugins" more than 100 Bundles will be selected and I get a lot of errors and exceptions.

So I came up with a new plugin from existing Jars which include the following three jars:

  • logback-classic-1.0.7
  • logback-core-1.0.7
  • com.springsource.slf4j.api-1.6.1 (I dont believe this is a good idea, but hey...)

If I create a new run configuration manually (not clicking "add required bundles" it works as expected but as far as I click "add required bundles" I come back to the more then 100 Bundles with a lot of errors (some Jetty stuff for example...)

So my Question is: How can I enable logback and slf4j in my OSGi application and use it within eclipse and configure it properly?

If you need more information please feel free to ask.

4

4 Answers

9
votes

AFAIK there are some issues in the bundle manifest header in the current official Logback/SLF4J jars. However, you only need the following three jars/bundles. No other are required for the basic functionality.

  • SLF4J API
  • Logback Core
  • Logback Classic

At Eclipse we put the bundles in Orbit for re-use across projects. We apply some modifications to the manifest header that we think are beneficial. For example, we deliver the actual SLF4J binding as a fragment to avoid the circular dependency of the original SLF4J API jar.

Here are the download links to the bundles:

You may also want:

Please note that "Add Required plugins" is not smart enough. It may select too many or too few plug-ins. Sometimes service API is delivered in one bundle but the actual service implementation is delivered in a second bundle. It may not select that bundle.

2
votes

There is a checkbox saying something like 'Resolve optional imports'. It's on by default, but that pretty much always results in the behaviour you describe, that it wants to add everything.

Switching that off should help. also, PDE tends to add a lot of fragments that are not needed.

All in all, I rarely trust Eclipse with adding the 'right' bundles for runtime. I just use 'validate' and add whatever is needed manually, and check again. It might take a few minutes but figuring out what went wrong when you leave it to PDE can take hours.

0
votes

Not sure about logback but you may also want to try pax logging. Just install pax logging api and pax logging service and it should work. There is also a documentation how to set it up in eclipse.

-1
votes

If you want it to use with Eclipse Equinox, you could try the Eclipse-BuddyPolicy. This enables one plugin to load all classes from another plugin without importing it explicitly. This may solve your problem.

Add to your Manifest from the bundle with the jars:

Eclipse-BuddyPolicy: dependent

and to the bundle using the logging

Eclipse-RegisterBuddy: com.other.plugin

see http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fbundle_manifest.html