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

家住魔仙堡 提交于 2019-12-10 17:12:40

问题


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.


回答1:


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.




回答2:


Late to the game but I needed support for log4j2 in Wildfly and thought I would share some details for anyone else facing this. This solution will let you configure logging through standalone.xml, that is no log4j2.xml will be used/picked up. The solution I chose is to bridge log4j2 to slf4j. Wildfly supports slf4j out-of-the-box. For the advanced users the solution in one sentence is 'Create a Wildfly module and use it', for others like me the solution follows a bit more detailed below...

First create a wildfly module, I will not give all the details how this is done, but my module.xml ended up looking like this

<module xmlns="urn:jboss:module:1.1" name="org.apache.logging.log4j2">
<resources>
    <resource-root path="log4j-api-2.11.1.jar"/>
    <resource-root path="log4j-to-slf4j-2.11.1.jar"/>
</resources>

<dependencies>
    <module name="org.slf4j" />
</dependencies>
</module>

(Basically make sure this xml is in your module path, typically you would add it at $JBOSS_MODULEPATH/org/apache/logging/log4j2/main/module.xml, and add the referenced jar-files in the same directly as well)

Next step is to add a dependency to this module from my application. This is accomplished using a jboss-deployment-structure.xml. If you are not familiar with this file please look it up. Add a line like this

<module name="org.apache.logging.log4j2" export="true" />

You might also need to exclude log4j-api-2.11.1.jar from your application deployment, I am not 100% sure it is needed but I always do when I create a module.

After this you should be able to see log4j2 messages in your server.log

I hope this can help someone out there!




回答3:


There is no module org.apache.logging.log4j. The module name you should be using is org.apache.log4j.

I'm not sure why you don't want to use CLI, but you could also set the add-logging-api-dependencies to false.

/subsystem=logging:write-attribute(name=add-logging-api-dependencies, value=false)


来源:https://stackoverflow.com/questions/35402632/how-to-setup-log4j2-for-an-application-deployed-in-wildfly-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!