When I test my application with JUnit, it is printing the log as specified by layout pattern in log4j2.xml
, but when I deploy my application
in WildFly 9, I am no more getting the same format. Even the log level in Log4j2 is also not reflecting while deployed in server.
JUnit log example:
2016-02-15 11:14:16,314 DEBUG [main] b.t.r.c.XAPool - a connection's state changed to IN_POOL, notifying a thread eventually waiting for a connection
Server log example:
11:11:33,796 INFO [org.quartz.core.QuartzScheduler] (ServerService Thread Pool -- 89) Scheduler quartzScheduler_$_anindya-ubuntu1455514892022 started.
Log4j2.xml:
<Configuration status="WARN" name="myapp" monitorInterval="5">
<Appenders>
<RollingFile name="RollingFile" fileName="${myapp.log-dir}/myapp.log"
filePattern="${myapp.log-dir}/$${date:yyyy-MM}/myapp-%d{MM-dd-yyyy}-%i.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
<DefaultRolloverStrategy max="100">
<Delete basePath="${myapp.log-dir}" maxDepth="2">
<IfFileName glob="*/myapp-*.log">
<IfLastModified age="7d">
<IfAny>
<IfAccumulatedFileSize exceeds="1 GB" />
<IfAccumulatedFileCount exceeds="1" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.company.myapp" level="trace" additivity="false">
<AppenderRef ref="RollingFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
While starting the server, I am providing below starup properties as JAVA_OPTS:
export JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active='qa' -Dlog4j.configurationFile=/home/anindya/1.0/log4j2.xml -myapp.log-dir=/home/anindya/log -Dorg.jboss.logging.provider=log4j"
I have no specific setup in web.xml
as it is Servlet 3.1 container. But I have a jboss-deployment-structure.xml
in my WEB-INF
as below:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.apache.logging.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>
And finally, here are my classpath dependencies (only the relevant parts are mentioned here):
With all of the above setup, I am still not able to configure Log4j2 in WildFly environment according to my log4j2.xml
. Can someone please help?
NOTE: I am running WildFly in standalone mode and I would like to avoid using jboss-cli
.
There are many ways to use Log4j2 configuration in you application. Using a configuration file written in XML, JSON, YAML or properties file. Programmatically, by creating a configuration factory and configuration implementation. Programmatically, by calling APIs exposed in the configuration interface.
Configuration of Log4j 2 can be accomplished in 1 of 4 ways: Through a configuration file written in XML, JSON, YAML, or properties format. Programmatically, by creating a ConfigurationFactory and Configuration implementation.
JBoss Deployment Structure File xml is a JBoss specific deployment descriptor that can be used to control class loading in a fine grained manner. It should be placed in the top level deployment, in META-INF (or WEB-INF for web deployments). It can do the following: Prevent automatic dependencies from being added.
If you are packaging log4j-core, you should exclude any module dependency on the log4-api provided by WildFly and instead package log4j-api in your deployment. CVE-2021-4104 Recently Red Hat reported another security vulnerability affecting Apache Log4j, in this case Log4j 1.
Let's look at how easy it is to deploy our new WildFly application through the console. Firstly, we log in to the console using the admin user and password set earlier. Click Add -> Upload Deployment -> Choose a file… Secondly, we browse to the target directory for the Spring Boot application and upload the WAR file.
WildFly does not ship Apache Log4j 1 itself, but the org.jboss.logmanager:log4j-jboss-logmanagerartifact we ship shades the Log4j 1 classes, including JMSAppender. JMSAppenderhas been present in WildFly or JBoss AS at least as far back as JBoss AS 7.1, and probably much farther.
The Log4j 2 API is a logging facade that may be used with the Log4j 2 implementation, but may also be used in front of other logging implementations such as Logback. Improved performance: In multi-threaded scenarios the Asynchronous Loggers have a fairly higher throughput than Log4j and Logback.
I managed to get it working by using the below jboss-deployment-structure.xml
.
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.apache.logging.log4j" />
</exclusions>
<exclude-subsystems>
<subsystem name="logging"/>
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
All I had to do is to exclude the logging subsystem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With