How to configure log4j in your ear ?
This article is now obsolete. We recommend checking these resources to learn bout log4j configuration with JBoss / WildFly:
Have you got exceptions when tried to configure log4j at application level ? this is due to the fact that JBoss ships already with log4j classes: you need to isolate the classes from other deployments. Without isolation, you can see common errors like ClassCastException, IllegalAccessErrors, VerifyErrors and in general, strange behavior.
Here’s how to do it:
Create a log4j.properties file and package it with your application and make it visible to JBoss classpath (see image at the bottom). The log4j.properties has just a minimal content:
log4j.logger.com.sample=TRACE
Within your application you need to add the appropriate version of log4j jar (this is important because you will have to ensure that log4j initializes separately for your application and picks up the log4j.properties file). Place this log4j jar file in the lib folder of your EAR (as depicted in the picture).
Finally you need to enable classloader isolation for your application. In order to achieve classloader isolation you need to add an entry in jboss-app.xml file and place it in the META-INF folder of the EAR. The jboss-app.xml looks like:
<jboss-app>
<loader-repository>
org.myapp:loader=MyClassLoader
<loader-repository-config>java2ParentDelegation=false </loader-repository-config>
</loader-repository>
</jboss-app>
This is how your application EAR should look like:
Recommended Articles
Mastering Log4j2 Configuration and Usage with WildFly - A Comprehensive Tutorial
Learn how to configure and utilize Log4j2 in your WildFly deployments. Explore advanced features like Asynchronous Logging, Filtering, and Custom Plugins.
Securing Your Java Applications from Log4j2 Vulnerabilities - CVE-2021-44228 and Beyond
Learn how to check for and mitigate Log4j2 vulnerabilities in your applications. Upgrade to version 2.17 or remove JMSAppender.
How to configure SLF4J in WildFly applications
Learn how to configure SLF4J 2.x in WildFly applications: exclude the built-in logging subsystem, wire up Log4j2 or Logback, and use the new Fluent Logging API.
Migrate JBoss Applications from Log4j1 to Log4j2: A Step-by-Step Guide
Learn how to migrate your Java applications running with Log4j 1.x to Log4j 2.x with WildFly/JBoss EAP. Get expert guidance on migration options and best practices.