Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 286
Anindya Chatterjee Avatar asked Feb 15 '16 06:02

Anindya Chatterjee


People also ask

How do you implement Log4j2?

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.

What is Log4j2 configuration file?

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.

What is JBoss deployment structure XML?

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.

Should I include log4j-api in my WildFly deployment?

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.

How do I deploy a Wildfly Application through the console?

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.

What version of Apache Log4j does WildFly ship?

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.

What is the Log4j 2 API?

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.


1 Answers

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.

like image 75
Anindya Chatterjee Avatar answered Oct 18 '22 07:10

Anindya Chatterjee