如何为WildFly 9中部署的应用程序设置Log4j2?

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

JUnit日志示例:

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

在启动服务器时,我提供了以下starup属性作为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”

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

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

最后,这是我的类路径依赖(这里只提到相关部分):

> hibernate-5.0.7.Final依赖项
> jbpm-6.3.0.Final依赖项
> 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

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

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

解决方法:

我设法通过使用下面的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>

我所要做的就是排除日志记录子系统.

上一篇:log4j2


下一篇:java – 我可以使用log4j 1.x和log4j.properties文件异步运行我的日志吗?