Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide a log4j.properties to an OSGI Bundle (Eclipse-Plugin)?

Project setup:

  1. Logging-1.0.jar
    • contains a Logger.class which uses slf4j/log4j
    • depends on slf4j-api.jar, slf4j-log4j.jar, log4j.jar
  2. LoggingOSGI-1.0.jar
    • wraps the logging project
    • contains an Activator and MANIFEST.MF
    • lib/ contains logging-1.0.jar, slf4j-api.jar, slf4j-log4j.jar, log4j.jar
    • jars from lib/ are added to classpath and packages from logging-1.0.jar are exported
  3. SomeBundle-1.2.jar
    • contains an Activator and MANIFEST.MF
    • has a dependency on LoggingOSGI-1.0.jar

Accessing the Logger class from SomeBundle works, but the logging project can't find the log4j.properties (log4j:WARN No appenders could be found for logger).

Questions:

  • Where do i have to place the log4j.properties?
  • Any ideas what i could try? (already tried: different directories, Eclipse-Buddies, -Dlog4j.configuration as VM argument)
  • Would be an extension point, which tells the logging project the location of the log4j.properties, a good solution?
like image 429
Absurd-Mind Avatar asked Aug 24 '12 11:08

Absurd-Mind


2 Answers

When I last tried this around six years ago, the solution turned to be to create a fragment bundle with the log4j.properties file, and then to attach that fragment (via the Fragment-Host manifest header) to the bundle that loads the logging library ("Logging-1.0.jar," in your case). It felt like a lot of project structure, build time, and deployment overhead for what seems like such a simple goal.

See section 3.14 of the OSGi Service Platform Core Specification for more detail on fragment bundles.

An alternate idea is to consider using the Configuration Admin Service to designate the path to a logging configuration file on disk, outside of your bundles. That would require augmenting your logging library to look up a configuration (or, better, listen for one) and then pass that configuration through to the logging implementation.

I would also be remiss to not point out the OSGi Log Service, specified in section 101 of the OSGi Service Platform Service Compendium.

like image 71
seh Avatar answered Oct 05 '22 15:10

seh


To solve my problem i added this code to the Activator of the LoggingOSGI-1.0 which configures log4j. The file path is taken from a System property: -Dlog4j.configuration=path/to/log4j.properties.

Still interested in other approaches or opinions to this solution.

private static final String LOG4J_CONFIG_KEY = "log4j.configuration";

public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;

    if (System.getProperties().containsKey(LOG4J_CONFIG_KEY)) {
        String file = System.getProperties().getProperty(LOG4J_CONFIG_KEY);
        PropertyConfigurator.configure(file);
    }
}
like image 35
Absurd-Mind Avatar answered Oct 05 '22 15:10

Absurd-Mind