Log4j2 api cannot find Log4j2 kernel in OSGi environment

I am trying to use the log4j2 OSGi packages, but it seems that the log4j2 api cannot find the log4j2 kernel in the OSGi environment. I constantly get the following exception:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console 

I found the same exception that was discussed in several places, but still I could not understand this problem. Isuspect I get this problem because the log4j2 api cannot find log4j-provider.properties inside the META-INF directory of the log4j2 kernel. Is there a clue why I am getting this exception and how can I fix the problem? (If anyone has the correct pom file for adding log4j dependencies and linking, please share it with me)

These are the dependencies that I used

  <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.2</version> </dependency> 

I am using apache felix as a bundle plugin. This error occurs because the resources inside the META-INF log4j2-core, especially for the log4j-providoer.properties file, are not visible to the log4j api.

Thanks!

+6
source share
5 answers

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.

+7
source

Log4j is not suitable for OSGi. Fortunately, there is a good replacement in replacing pax-logging . In your kit, you use log4j api or any other supported apis (I prefer slf4j-api). Then you deploy pax logging in your OSGi infrastructure and in your bundle. You can configure pax logging using the standard log4j configuration. Thus, it is very easy to use. If you want to get started very simply, you can just install apache karaf and deploy your package. Karaf already includes fully configured pax logging.

0
source

Try changing the name of the jar file that does not have the main word (for example: log4j-zore) and try again

0
source

You need to specify the dependencies associated with OSGI with your package in META-INF/MANIFEST.MF , adding the following dependencies:

Require-Bundle: org.apache.logging.log4j.core;org.apache.logging.log4j.api

0
source

For me, this error is resolved:

  • ensure that the log4j-api package is actually activated - and not only allowed - before calling the registrar. I did this by setting it to Auto-Start with a low entry level.
  • let log4j-api import the log4j-core classes (as mentioned in @@) to make sure it finds log4j-core. The cleanest way to do this is to use a snippet. Instead of dynamically importing, you can also add this to the manifest:

    Require-package: org.apache.logging.log4j.core

I also use a snippet for log4j-core so that it can find the log4j2.xml configuration file, but if this snippet is not used, log4j will show another error message.

In addition, I found that it is not necessary to activate the log4j-core package (only allowed), but note that this means that Activator cannot find the custom log4j2 plugins.

0
source

Source: https://habr.com/ru/post/986546/


All Articles