Using Log4j2 from the native OSGi framework

We use Apache Felix (version 4.4.1)

OSGi as built-in infrastructure. The main application providing the infrastructure is Log4j2 (version 2.0.1)

as a logging framework. There are dependencies on log4j-api and log4j-core. Of course, when the main application is running, Log4j2 will be loaded and configured. All log messages triggered in the main body are correctly written to the configured applications.

Now the point. All loaded packages must have the same configuration and for this the same LoggingContext as the main application. What I was trying to do was provide all Log4j2 packages (from log4j2-api and log4j-core) by putting them in the frame config as org.osgi.framework.system.packages.extra

. But when the packages are loaded, the log4j2 packages do not show up in the package list to post like the other provided packages do, and when the package class tries to access the Log4j2 classes, an exception will be thrown.

All packages are configured the same and have Log4j2 dependencies on log4j2-api and log4j2-core with provided scope. When I go through the MANIFEST.MF the Log4j2 packages used are in Import-Packages

part

So my questions.

Why doesn't exporting Log4j2 packages work this way? How do I configure my environment so that I can use the same LoggingContext in my bundles and main application?

Here is some of the OSGi config from Java code:

Map<String, String> temp = new HashMap<String, String>();
// setting parameter that the felix cache is new initialized on each
// start of the application
temp.put(org.osgi.framework.Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
// setting the packages that are provided by the main system and can
// be used by the osgi bundles
temp.put(org.osgi.framework.Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
// have to export these explicitly as the automatic export does
// not work for this internal java packages
+ "com.sun.org.apache.xerces.internal.dom,"
+ "com.sun.org.apache.xerces.internal.jaxp,"
+ "com.sun.org.apache.xerces.internal.util,"
// export the looging packages
+ "org.apache.log4j," + "org.apache.logging.log4j," +   "org.apache.logging.log4j.message,"
+ "org.apache.logging.log4j.simple," + "org.apache.logging.log4j.spi,"
+ "org.apache.logging.log4j.status," + "org.apache.logging.log4j.util,"
+ "org.apache.logging.log4j.impl," + "org.apache.logging.log4j.appender,"
+ "org.apache.logging.log4j.appender.db," + "org.apache.logging.log4j.appender.db.jdbc,"
+ "org.apache.logging.log4j.appender.db.jpa," + "org.apache.logging.log4j.appender.db.jpa.converter,"
+ "org.apache.logging.log4j.appender.jms," + "org.apache.logging.log4j.appender.rewrite,"
+ "org.apache.logging.log4j.appender.rolling," + "org.apache.logging.log4j.appender.rolling.action,"
+ "org.apache.logging.log4j.appender.routing," + "org.apache.logging.log4j.async,"
+ "org.apache.logging.log4j.config," + "org.apache.logging.log4j.config.json,"
+ "org.apache.logging.log4j.config.plugins," + "org.apache.logging.log4j.config.plugins.osgi,"
+ "org.apache.logging.log4j.config.plugins.processor,"
+ "org.apache.logging.log4j.config.plugins.util," + "org.apache.logging.log4j.config.plugins.visitors,"
+ "org.apache.logging.log4j.config.status," + "org.apache.logging.log4j.config.xml,"
+ "org.apache.logging.log4j.config.yaml," + "org.apache.logging.log4j.filter,"
+ "org.apache.logging.log4j.impl," + "org.apache.logging.log4j.jackson,"
+ "org.apache.logging.log4j.jmx," + "org.apache.logging.log4j.layout,"
+ "org.apache.logging.log4j.lookup," + "org.apache.logging.log4j," + "org.apache.logging.log4j.net,"
+ "org.apache.logging.log4j.net.jms," + "org.apache.logging.log4j.net.server,"
+ "org.apache.logging.log4j.net.ssl," + "org.apache.logging.log4j.pattern,"
+ "org.apache.logging.log4j.selector," + "org.apache.logging.log4j.tools,"
+ "org.apache.logging.log4j.util," + "sun.misc," + "javax.jms," + "javax.mail,"
+ "javax.mail.internet," + "javax.mail.util," + "javax.persistence," + "javax.servlet,"
+ "com.sun.xml.internal.bind");
temp.put(FelixConstants.LOG_LEVEL_PROP, "4");
OSGI_CONFIG = Collections.unmodifiableMap(temp);

      

Dependencies on log4j2 libs in bundles are defined as follows:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
</dependency>

      

+3


source to share


3 answers


We found a solution that works for us with version 2.0.1. Instead of installing the required log4j packages in Import-Package

the MANIFEST.MF part, we added them to the definition DynamicImport-Package

. After that, the classes are dynamically connected as needed and all OSGi packages can use the same LoggerContext.



+2


source


I have log4j2 running in osgi in a relatively light configuration.

You must create TWO fragments.

  • 1st snippet should contain log4j-api as host. It should contain one file: META-INF / log4j-providers.properties (take it out from log4j2-core)
  • The second snippet should contain log4j-core as host. It should contain one file: log4j2.xml (or whatever config file you want)


This works by simply deleting the log4j-api and log4j-core log files to the plugins directory (p2 if you are using equinox). I have the following chain working fine:

commons-logging (jcl-over-slf4j) → slf4j (slf4j-api) → log4j-slf4-impl → log4j-api → log4j-core.

Note: slf4j version 1.7.7 log4j2 version 2.1

+2


source


I have log4j working fine in OSGi (Eclipse).

As @Fenris Ulth mentioned, you can create a snippet for log4j-core containing the log4j2.xml config file. In log4j2 version 2.3 I found that I had to activate the log4j-api package before my code started using log4j to log messages, otherwise I got the error described here . I did this by setting it to Auto-Start with a low start.

+1


source







All Articles