How to setup Log4j2 for an application deployed in WildFly 9?

Anindya Chatterjee picture Anindya Chatterjee · Feb 15, 2016 · Viewed 10.2k times · Source

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):

  • hibernate-5.0.7.Final dependencies
  • jbpm-6.3.0.Final dependencies
  • spring-4.2.4.RELEASE dependencies
  • commons-logging-1.2.jar
  • log4j-1.2-api-2.5.jar
  • log4j-api-2.5.jar
  • log4j-core-2.5.jar
  • log4j-jcl-2.5.jar
  • log4j-slf4j-impl-2.5.jar
  • log4j-web-2.5.jar
  • jboss-logging-3.3.0.Final.jar

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.

Answer

Anindya Chatterjee picture Anindya Chatterjee · Feb 18, 2016

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.