如何为WildFly 9中部署的应用程序设置Log4j2? [英] How to setup Log4j2 for an application deployed in WildFly 9?

查看:121
本文介绍了如何为WildFly 9中部署的应用程序设置Log4j2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用JUnit测试应用程序时,它会按照log4j2.xml中的布局模式指定的方式打印日志,但是当我部署应用程序时 在WildFly 9中,我不再获得相同的格式.甚至在服务器中部署Log4j2中的日志级别时,也不会反映出来.

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日志示例:

JUnit log example:

2016-02-15 11:14:16,314调试 [main] b.t.r.c.XAPool -连接的 状态更改为IN_POOL,通知线程最终等待 连接

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

服务器日志示例:

11:11:33,796信息 [org.quartz.core.QuartzScheduler] (ServerService 线程池-89)调度程序 crystalScheduler _ $ _ anindya-ubuntu1455514892022已启动.

11:11:33,796 INFO [org.quartz.core.QuartzScheduler] (ServerService Thread Pool -- 89) Scheduler quartzScheduler_$_anindya-ubuntu1455514892022 started.

Log4j2.xml:

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>

在启动服务器时,我在下面的starup属性中提供了JAVA_OPTS:

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"

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"

我在web.xml中没有特定的设置,因为它是Servlet 3.1容器.但是我的WEB-INF中有一个jboss-deployment-structure.xml,如下所示:

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.最终依赖项
  • jbpm-6.3.0.最终依赖项
  • spring-4.2.4.RELEASE依赖项
  • 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
  • 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

使用上述所有设置,我仍然无法根据我的log4j2.xml在WildFly环境中配置Log4j2.有人可以帮忙吗?

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?

注意:我以独立模式运行WildFly,我想避免使用jboss-cli.

NOTE: I am running WildFly in standalone mode and I would like to avoid using jboss-cli.

推荐答案

我设法通过使用下面的jboss-deployment-structure.xml使它工作.

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.

这篇关于如何为WildFly 9中部署的应用程序设置Log4j2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆