The log4j-core package registers the packet listener when the activator starts and starts searching for log plugins, and if something is found, it performs a sequence of operations similar to the usual Logger initialization (not very idiomatic OSGi stuff, and I'm not sure if it works, but it apparently at least Log4jContextSelector and LoggerContextFactory ), just to make sure of this, you installed and run the log4j-core package and confirmed that nothing has changed?
Update:
I did some testing and found what is an acceptable solution / workaround for log4j2 OSGi problems. But, as someone else recommended, I would use slf4j, pax-logging or just the OSGi logging service (the simpler of the group).
@Grant, you have two separate things that need to be fixed:
1. As you described, the error โLog4j2 could not find a logging errorโ is caused by the fact that the log4j2-api package cannot find the log4j-provider.properties file and , after this is fixed, log4j2-api cannot find the log4j2-core classes log4j2-core (it has a different package and log4j2-api does not have a special Import-Package: for these classes).
The workaround for this is to create a small snippet package for log4j2-api (I call my log4j-api-config.jar) with this .properties file in META-INF and a manifest that forces dynamic imports:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Log4j API configurator Bundle-SymbolicName: org.apache.logging.log4j.apiconf Bundle-Version: 1.0.0 Bundle-Vendor: None Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.2 Fragment-Host: org.apache.logging.log4j.api DynamicImport-Package: *
Here I import *, you can improve it by adding the required subset of log4j2-core packages, which is required by log4j2-api .
This error will disappear, but log4j will notice that you did not provide the log4j2 configuration file, and then a fix (only in this case).
2. At this point, Felix will display this:
log4j2.xml not found by org.apache.logging.log4j.core ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
and I suppose you might want to add your own log4j2.xml file without messing up the original log4j2-core.jar . You can do this by creating another fragment fragment, this time hosted by log4j2-core , with the log4j2.xml configuration file in the root and a simple manifest:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Log4j Core configurator Bundle-SymbolicName: org.apache.logging.log4j.coreconf Bundle-Version: 1.0.0 Bundle-Vendor: None Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.2 Fragment-Host: org.apache.logging.log4j.core
I used this simple log4j2.xml configuration during my tests:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
With this, you no longer need the "bridge" that you described below, and you just need a simple Import-Package: org.apache.logging.log4j to use log4j from your package.
Update 2:
It is important to note that these two fragments are NOT dependencies of the source packages (there is no need to modify the log4j banks or even your packages to add import / export), so the source packages and your own custom ones remain unchanged. In addition, they are independent of the source package, it is just a basic jar archive with a manifest and an additional text file, without code, there is no need for Import-Package or Export-Package.
You just need to install each fragment after installing the host package.
I created both fragments manually, starting with an empty jar and copying the properties file inside the archive and modifying MANIFEST.MF with a text editor, you can create both of them with this pom.xml, do not forget to copy log4j- provider.properties where pom.xml is located.
For the log4j2-api fragment:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>my.group</groupId> <artifactId>log4j2-api-config</artifactId> <version>1.0</version> <name>log4j2-api-config</name> <packaging>bundle</packaging> <properties> <java-version>1.7</java-version> </properties> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.0.0</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>org.apache.logging.log4j.apiconf</Bundle-SymbolicName> <Bundle-Name>Log4j API Configurator</Bundle-Name> <Bundle-Version>1.0.0</Bundle-Version> <Fragment-Host>org.apache.logging.log4j.api</Fragment-Host> <DynamicImport-Package> *;resolution:=optional </DynamicImport-Package> </instructions> </configuration> </plugin> </plugins> <resources> <resource> <directory>.</directory> <includes> <include>log4j-provider.properties</include> </includes> <targetPath>META-INF</targetPath> </resource> </resources> </build> </project>
Change this pom where necessary (included file, package names) to generate another one with log4j2-core configuration.