Logback and conditional processing of configuration files in an OSGi environment

Recently I had to make use of conditional processing
in logback’s config files. Because the application
runs in an OSGi environment I had to spend some time
in order to get everything working as intended.

Problem analysis

As stated in the Logback documentation
conditional processing in configuration files requires the
Janino library. When running in an OSGi
environment (e.g. an Eclipse RCP application) simply placing
commons-compiler.jar and janino.jar on your application’s class path is not
sufficient enough. You will get the following exception:

This exception occurs because in an OSGi environment the class loader of the
org.codehaus.janino bundle can not load classes from the ch.qos.logback.core
bundle. See here and
here
for a primer on OSGi class loading.

Solution

The org.codehaus.janino bundle would need a “Require-Bundle:
ch.qos.logback.core” directive in it’s manifest file in order for the bundle
class loader to be able to load classes from the ch.qos.logback.core bundle.
This can be achieved by extending the org.codehaus.janino bundle via a fragment
(e.g. ch.qos.logback.core.janino.fragment). Below is the manifest of the
fragment:

The bundle name org.cohehaus.janino in the bundle fragment directive is a typo
in the upstream project. I am currently evaluating if it’s possible to push
such a fragment to the logback upstream project.

Update #1 (10. Apr. 2016)

My Pull Request over at
GitHub has been accepted. The logback-janino-fragment is now part of the
ch.qos.logback.contrib project. You can download the artifact / fragment
e.g. here.