I'm trying to use log4j2 OSGi bundles, but it seems log4j2 api cannot find log4j2 core in an OSGi environment. I'm continuously getting 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 discussed in few places but still I could not figure out this issue. Isuspect I'm getting this issue because log4j2 api cannot find the log4j-provider.properties inside the META-INF directory of log4j2 core.
Is there any clue why I'm getting this exception and how can I correct the issue ?
(If anybody has correct pom file for adding log4j dependencies and bundling please share it with me)
These are the dependencies I have 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 use apache felix as the bundle plugin. This error occures because resources inside the META-INF of log4j2-core specially the log4j-providoer.properties file is not visible to log4j api.
Thanks!
The log4j-core bundle registers a bundle listener when the Activator is started and starts searching for log plugins and if something is found it performs a sequence of operations similar to the usual Logger initialization (not really idiomatic OSGi stuff and i'm not sure it works, but it seems to set at least Log4jContextSelector and LoggerContextFactory), just to be sure of it, did you install and start the log4j-core bundle and verified that nothing changed?
Update:
I did some testing and found what is an acceptable solution/workaround for log4j2 OSGi issues. But as someone else recommended, alternatively i would use slf4j, pax-logging or simply the OSGi Log Service (the simpler of the bunch).
#Grant, you have 2 separate things that need to be fixed:
1. As you described, the "Log4j2 could not find a logging implementation" error is caused by the fact that the log4j2-api bundle is unable to find the log4j-provider.properties file and, after that is fixed, log4j2-api cannot find the log4j2-core classes (it's a different bundle and log4j2-api doesn't have a specific Import-Package: for those classes).
The workaround for this is to create a small fragment bundle for log4j2-api (i called mine log4j-api-config.jar) with that .properties file in META-INF and a manifest that forces a dynamic import:
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: *
I'm importing * here, you can improve it adding the required subset of log4j2-core packages that log4j2-api needs.
With this, that error will disappear, but log4j will notice that you didn't provide a log4j2 configuration file, next thing to fix (only if you care 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 could want to add your own log4j2.xml without messing with the original log4j2-core.jar. You can do this creating another fragment bundle, this time hosted by log4j2-core, with just a 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 will not need that sort of "bridge" bundle you described below anymore, and you'll just need a simple Import-Package: org.apache.logging.log4j to use log4j from your bundle.
Update 2:
Important to note that the two fragments are NOT dependencies of the original bundles (no need to modify log4j jars or or even your bundles to add import/export), so the original bundles and your own custom ones will remain untouched.
Also, they don't depend on the original bundle either, they are just basic jar archive with a manifest and an additional text file, no code, no need for Import-Package or Export-Package.
You just need to install each fragment after their host bundle is installed.
I've created both fragments manually starting from an empty jar and copying inside the archive the properties file and modifying the MANIFEST.MF with a text editor, you can create them both with this pom.xml, remember 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>
Modify this pom where appropriate(included file, bundle names) to generate the other one with the log4j2-core configuration.
Log4j is not suitable for an OSGi environment. Luckily there is a nice drop in replacement pax-logging. In your bundle you use the log4j api or any other of the supported apis (I prefer slf4j-api). Then you deploy pax logging to your OSGi framework and your bundle.
You can configure pax logging using a standard log4j config. So it is very easy to use. If you want a really easy start you can simply install apache karaf and deploy your bundle to it. Karaf already includes a fully set up pax logging.
Try changing the name of the jar file something does not contain core word (eg:log4j-zore) and try again
You need specify OSGI related dependencies on Your bundle in META-INF/MANIFEST.MF by add the following dependencies:
Require-Bundle: org.apache.logging.log4j.core;org.apache.logging.log4j.api
For me this error is resolved by:
ensuring that the log4j-api bundle is actually activated - and not just resolved - before calling the logger. I did this by setting it to Auto-Start with a low start level.
let log4j-api import classes of log4j-core (as mentioned by #uraime) to make sure it finds log4j-core. The cleanest way to do this is using a fragment. Instead of dynamic import you could also add this to the manifest:
Require-Bundle: org.apache.logging.log4j.core
I also use a fragment for log4j-core to let it find my log4j2.xml configuration file, but when not using this fragment, log4j will show a different error message.
Additionally I found that it's not required for the log4j-core bundle to be activated (only resolved) but note that this does mean that the Activator cannot find custom log4j2 plugins.
Related
Migrating an existing project to maven and wildfly and had to move some files around creating the following situation.
core.jar
ejb1.jar
ejb2.jar
With the ejb-jar.xml for ejb1.jar having the following:
<ejb-jar>
<enterprise-beans>
<session id="Value">
..
<home>path.to.ejb1Home</home>
<remote>path.to.ejb1</remote>
...
The classes referenced in the home and remote tags have moved to be inside core.jar
Meanwhile ejb2.jar has dependencies on other parts of core.jar.
What looked like the easiest resolution was to create a global module and place core.jar in it, creating the appropriate module.xml and entry in standalone.sh to make it accessible to all deployments.
module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="path.to">
<resources>
<resource-root path="core-1.0.jar"/>
</resources>
<dependencies>
</dependencies>
</module>
With this setup, ejb2.jar deploys as expected but ejb1.jar throws the following error:
Caused by: java.lang.NoClassDefFoundError: Failed to link path.to.ejb1
(Module "path.to" from local module loader #4c40b76e (finder: local
module finder #2ea6137 (roots: /opt/wildfly/modules,
/opt/wildfly/modules/system/layers/base))): javax/ejb/EJBObject
You should carefully think about your requirement. Usually, you don't need to provide common resources, especially application interfaces, as module, especially as global module. Choose the right solution for your requirements, not a solutions that looks easy at first sight without knowing the implications.
I would recommend these possibilities in the order given (see the Class Loading in Wildfly Documentation for more information):
Reconsider your requirement for different EARs. Provide the core jar as library with your EAR.
Even with more than one EAR: provide the core.jar as library. This is no redundancy, this is just separation.
Consider not using a global module. Just install your custom module. Provide a jboss-deployment-structure.xml file, adding this module as additional dependency. If the module contains annotated classes or interfaces, which should be considered upon deployment, set the attribute annotations=true with the module dependency configuration.
Add a global module to your configuration file. Probably you can even use the `annotations' property, but this is something I don't know and I didn't check.
An example for the jboss-deployment-structure.xml file:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.3">
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
<deployment>
<dependencies>
<module name="my.custom.module" export="true" annotations="true"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
EDIT:
When installing modules, don't forget to provide transitive module dependencies to other modules your own module depends on. User your module.xml file to do so. In your case, you probably need at least a dependency to the javax.ejb.api module.
We want to exclude the modules\system\layers\base\javax\servlet\jstl\api\main\jboss-jstl-api_1.2_spec-1.1.2.Final.jar from our web application deployment (WAR file).
Hence we have the following configuration in src\main\webapp\WEB-INF\jboss-deployment-structure.xml:
<?xml version='1.0' encoding='UTF-8'?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="javax.servlet.jstl.api"/>
</exclusions>
<dependencies>
<module name="deployment.my-dependencies.jar"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
In the Wildfly log I see that my-dependencies.jar is added as a ModuleDependency. But when searching for javax.servlet.jstl.api I only see this:
2015-04-03 15:22:11,971 DEBUG [org.jboss.modules] (ServerService
Thread Pool -- 12) Module javax.servlet.jstl.api:main defined by local
module loader #1f7c9157 (finder: local module finder #2b29f6e7 (roots:
C:\Users\me\Documents\wildfly-8.2.0.Final\modules,C:\Users\me\Documents\wildfly-8.2.0.Final\modules\system\layers\base))
Why isn't the module excluded?
Update: It seems that modules that are part of a user dependency can not be excluded.
It seems like the mechanism doesn't work as described in the Wildfly documentation. I was not able to exclude that module.
Yep. I was trying to upgrade to Spring Framework to v4.3. It has upped some minimum dependency requirements. One such example is Jackson min version required is 2.6+
Wildfly loads jackson that comes packaged (v2.4.1 in Wildfly 8.2.1), and it won't be excluded using jboss-deployment-structure.xml.
I was trying to see if the upgrade did not involve making changes to the installed server which takes this upgrade out of source control.
i am working on a project where i utilize log4j2 logging.
while developing in intellij, all works fine and the logging is done as expected. the log4j2.xml is linked through java property passed to jvm on startup via intellij settings.
but once i try to run a standalone gradle built fat-jar, i'm experiencing the following problems:
java -Dlog4j.debug=true -Dlog4j.configurationFile=/home/aaa/log4j2.xml -jar /home/aaa/myjar-SNAPSHOT.jar
exceptions:
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
...
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
i don't even understand where those [thread]s come from, since i get the same error even while using a basic simplest config in my log4j2:
<?xml version="1.0" encoding="UTF-8" ?><Configuration status="WARN" monitorInterval="86400">
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="%-5p %d{yyyy-MM-dd HH:mm:ss.SSS} ${hostName} %c{1} %msg %throwable{7}%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
any thoughts are welcome. thanks.
in fatJar, dependencies can provide a log4j-provider.properties in the META-INF that cause this issue,
remove it in the gradle task :
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'project',
'Implementation-Version': project.version,
'Main-Class': 'com.sample.CLI'
}
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it).matching {
exclude 'META-INF/**.RSA'
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/log4j-provider.properties'
} } }
with jar
}
The problem is described here: https://issues.apache.org/jira/browse/LOG4J2-673
Unfortunately at the moment there only seems to be a solution for the maven-shade-plugin: https://github.com/edwgiz/maven-shaded-log4j-transformer
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}${appSuffix}</finalName>
<transformers>
...
<transformer
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
...
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
Kinda late to this party, but I thought I'd add my situation, in case it helps anyone.
I build my own fat jar out of a subset of classes and jars from the larger project. The essential class runs, but I was getting all the "Unrecognized format specifier" and all. Most of the answers involve mvn shade or other such, so that wasn't helping me. But poking around, I learned that log4j-web.jar also includes the Log4jPlugins.dat file, causing the problems. Removing that from the build, and all is good.
(And I thought my build script was so tricky, including all the jars by project name, eg all jars for "log4j.")
The LoggerContextFactory binds the Log4j API to its implementation. The Log4j LogManager locates a LoggerContextFactory by locating all instances of META-INF/log4j-provider.properties, a standard java.util.Properties file, and then inspecting each to verify that it specifies a value for the Log4jAPIVersion property that conforms to the version required by the LogManager.
Incase of fat jar, you can also explicitly specify log4j2 to use LoggerContextFactory in your application by:
System.setProperty("log4j2.loggerContextFactory", "org.apache.logging.log4j.core.impl.Log4jContextFactory")
or as specified in the log4j-provider.properties file included in your log4j-core jar.
When you run your application from an IDE, jar runs itself without embedding the dependencies and you don't have conflict of log settings. But when you convert the application into a fat jar then all the dependencies will be injected into your project's jar file and your log4j settings that come from external jar files (dependencies) may be conflicted while fatJar process merge them into a single artifact.
In this case i think your "Log4j2Plugins.dat" files may be conflicted. To be sure, you can open your fatJar file with a zip editor (ex: 7Zip), navigate to path in fatJar as below and delete one of the conflicted files (you can choose smallest one by size) from your fatJar. Run the fatJar and check the logging is working properly.
\META-INF\org\apache\logging\log4j\core\config\plugins\Log4j2Plugins.dat
Now we can check the dependencies (artifacts) and find which of them contain the "Log4j2Plugins.dat" files. So you can exclude the modules that have the file from your build tool and then your fatJar creation process will exclude the conflicted files and your new fatJar can start logging as expected.
In my case, my fatJar module imports some other modules from Spring Boot and when i exclude the conflicted logging libraries, my fatJar starts logging without any error.
configurations {
all*.exclude module: 'spring-boot'
all*.exclude module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
all*.exclude module: 'commons-logging'
}
I'm using Tycho to build and test some eclipse plugins. I have one bundle that has many platform specific fragments. I also have one test bundle that is using tycho-surefire-plugin to test the original bundle that has the platform specific fragments. However, Tycho is not including the current platform's fragment into the test runtime.
All of the platform specific fragments look like the win64 fragment manifest listed below. (There are actually six fragments in total, one for each platform combination I need to support.)
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Liferay AUI Upgrade Tool Win64
Bundle-SymbolicName: com.liferay.laut.win32.win32.x86_64;singleton:=true
Bundle-Version: 1.0.2.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Fragment-Host: com.liferay.ide.alloy.core
Eclipse-BundleShape: dir
Eclipse-PlatformFilter: (& (osgi.ws=win32)(osgi.os=win32)(osgi.arch=x86_64))
Bundle-Vendor: Liferay, Inc.
Example win64 fragment pom.xml's <build> section
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<configuration>
<environments>
<environment>
<os>win32</os>
<ws>win32</ws>
<arch>x86_64</arch>
</environment>
</environments>
</configuration>
</plugin>
</plugins>
</build>
When I try to execute my Tycho build and it runs the surefire test plugin (no matter which OS I try), the correct platform fragment is not added into the runtime.
I've seen various posts on stackoverflow about similar questions but in those cases the fragments loaded into the test runtime were not platform-specific fragments with OS filters.
This is a good question - but if you know the right trick, the solution is fortunately not complicated: Simply configure Tycho to include the feature which contains all fragments into the test runtime.
Create a feature in an eclipse-feature module which includes all
the native fragments. Make sure that the platform filters for each
plug-in is correct: On the Plug-Ins tab of the feature.xml editor,
you need to select the correct os/ws/arch that each fragment applies
to. This is some manual effort, but typically can re-use this
feature to include your fragments into a p2 repository/update site.
Include this feature into the test runtime with the following POM configuration:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<dependency-resolution>
<extraRequirements>
<requirement>
<type>eclipse-feature</type>
<id>fragment-containing-feature</id>
<versionRange>0.0.0</versionRange>
</requirement>
</extraRequirements>
</dependency-resolution>
</configuration>
</plugin>
A potential pitfall is the <environments> configuration of the eclipse-feature module: You don't need anything special for that module; just have the module inherit the <environments> configuration from the parent POM. Note that the parent POM shall configure all the environments your build supports - and only the fragment modules need to override the global configuration.
Is it possible to have a typical call to java.util.logging.Logger and have it route to Logback using SLF4J? This would be nice since I wouldn't have to refactor the old jul code line by line.
EG, say we have this line:
private static Logger logger = Logger.getLogger(MahClass.class.getName());
//...
logger.info("blah blah blah");
It would be nice to configure this to call through SLF4J.
It's very easy and not a performance issue anymore.
There are two ways documented in the SLF4J manual. There are also precise examples in the Javadocs
Add jul-to-slf4j.jar to your classpath. Or through maven dependency:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.0</version>
</dependency>
If you don't have logging.properties (for java.util.logging), add this to your bootstrap code:
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
If you have logging.properties (and want to keep it), add this to it:
handlers = org.slf4j.bridge.SLF4JBridgeHandler
In order to avoid performance penalty, add this contextListener to logback.xml (as of logback version 0.9.25):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<!-- reset all previous level configurations of all j.u.l. loggers -->
<resetJUL>true</resetJUL>
</contextListener>
...
</configuration>