带有xi:include的LOG4J 2 Web应用程序配置

时间:2019-04-17 12:56:06

标签: java xml logging web-applications log4j2

我正在尝试在Web应用程序中设置log4j 2。 我在web.xml中添加以下行

<context-param>
    <param-name>isLog4jAutoInitializationDisabled</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>isLog4jContextSelectorNamed</param-name>
    <param-value>false</param-value>
</context-param>

<listener><listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class></listener>

<filter><filter-name>log4jServletFilter</filter-name><filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class></filter><filter-mapping><filter-name>log4jServletFilter</filter-name><url-pattern>/*</url-pattern><dispatcher>REQUEST</dispatcher><dispatcher>FORWARD</dispatcher><dispatcher>INCLUDE</dispatcher><dispatcher>ERROR</dispatcher><dispatcher>ASYNC</dispatcher></filter-mapping>

如此处所述 https://logging.apache.org/log4j/2.x/manual/webapp.html

我设置了我的显示名称

<display-name>MYAPP</display-name>

在web.xml中

我在一个

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

我将这个log4j2.xml包含在我的WEB-INF中

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" strict="true" monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="${application.conf.dir}/MYAPP/log4j2.xml"/>
</Configuration>

$ {application.conf.dir} /MYAPP/log4j2.xml中的文件:

<?xml version="1.0" encoding="UTF-8"?>

<Appenders>
<RollingFile name="A3_MYAPP" fileName="${sys:application.log.dir}/MYAPP/MYAPP.log"
        filePattern="${sys:application.log.dir}/MYAPP/MYAPP_%d{yyyy-MM-dd}.%i.log.bz2">
    <PatternLayout>
        <Pattern>%d{yyyy/MM/dd HH:mm:ss,SSS} [%p] - %m%n</Pattern>
    </PatternLayout>
    <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="100 MB"/>
    </Policies>
    <DefaultRolloverStrategy fileIndex="nomax">
        <Delete basePath="${sys:application.log.dir}/MYAPP/">
            <IfAny>
                <IfAccumulatedFileSize exceeds="100 MB" />
                <IfLastModified age="30d" />
            </IfAny>>
        </Delete>
    </DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="MYAPP" additivity="false" level="debug">
    <AppenderRef ref="A3_MYAPP"/>
</Logger>
</Loggers>

部署Webapp时出现此错误:

07:10:54,792错误[stderr](ServerService线程池-151)[警告] log4j2.xml:5:68:包含操作失败,恢复为回退。资源错误以XML格式读取文件(href ='$%7Bapplication.conf.dir%7D / MYAPP / log4j2.xml')。原因:C:\ wildfly-14.0.1.Final \ standalone \ tmp \ vfs \ temp \ temp96a1091798388092 \ content-1f986d8fa374ea35 \ WEB-INF \ $ {application.conf.dir} \ MYAPP \ log4j2.xml(Ispossibile trovare il percorso规范

因为在C:\ wildfly-14.0.1.Final \ standalone \ tmp \ vfs \ temp \ temp96a1091798388092 \ content-1f986d8fa374ea35 \ WEB-INF \ $ {application.conf.dir} \ MYAPP \ log4j2中没有文件。 xml

在我看来,xi href中没有进行系统变量替换,它仍然存在 href ='$%7Bapplication.conf.dir%7D / MYAPP / log4j2.xml'

我什至试图写

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" strict="true" monitorInterval="30" xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="${sys:application.conf.dir}/MYAPP/log4j2.xml"/>
</Configuration>

获取此错误: 08:39:05,946错误[stderr](ServerService线程池-93)[致命错误] log4j2.xml:5:72:'href'属性值'$%7Bsys:application.conf.dir%7D / ADR_datex2 / log4j2 .xml”在语法上无效。应用转义规则后,该值既不是语法正确的URI也不是IRI。

所以我的问题是: 我如何让我的Web应用程序使用使用log4j-web模块的外部log4j2.xml文件,以便根据自己的意愿进行更改? xi:include可以替换使用系统环境/ java系统属性吗?

提前谢谢

p.s。 这些是启动时的Java系统属性

Setting JAVA property to "C:\PROGRA~1\Java\JDK-11~1.2\\bin\java"
===============================================================================
JBoss Bootstrap Environment

JBOSS_HOME: "C:\wildfly-14.0.1.Final"

JAVA: "C:\PROGRA~1\Java\JDK-11~1.2\\bin\java"

JAVA_OPTS: "-Dprogram.name=standalone.bat  -server -Duser.timezone=America/Santiago -Duser.country=CL -Duser.language=en -Dlog4j.debug=true -Dapplication.conf.dir=/prd/tps/conf/-Djboss.userconf.dir=/prd/tps/conf/ -Dapplication.log.dir=/tmp/log/-Djboss.userlog.dir=/tmp/log/ -Dapplication.data.dir=/prd/tps/data/ --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED --add-exports=jdk.unsupported/sun.reflect=ALL-UNNAMED --add-modules=java.se"
===============================================================================

0 个答案:

没有答案