2014-09-05 301 views
2

Log4j2支持像Logback这样的文件包含机制吗?这是对于包括一个配置文件的一部分从另一个文件(包含追加程序,记录器等)Log4j2文件包含:<include>和<included>类似于Logback

作为一个供参考 - 在这里是如何工作的logback中:

Joran支持包括从另一个配置文件的部分文件。这是通过声明一个元素来完成,如下所示:

例如:文件包括(的logback-实例/ SRC /主/爪哇/章节/配置/ containingConfig.xml)

<configuration> 
<include file="src/main/java/chapters/configuration/includedConfig.xml"/> 

<root level="DEBUG"> 
<appender-ref ref="includedConsole" /> 
</root> 

目标文件必须将其元素嵌套在元素中。例如,一个是ConsoleAppender可被声明为:

例如:文件包括(的logback-实例/ SRC /主/爪哇/章节/配置/ includedConfig.xml)

<included> 
<appender name="includedConsole" class="ch.qos.logback.core.ConsoleAppender"> 
<encoder> 
    <pattern>"%d - %m%n"</pattern> 
</encoder> 
</appender> 
</included> 

回答

2

甲略有不同,但类似的机制存在于Log4j2中,它支持XInclude。 详情请参阅https://issues.apache.org/jira/browse/LOG4J2-341。 (这需要更好记录......)

+0

我试过这种方法 - 。但是,XInclude的问题是引用的文件始终是强制性的。当围绕日志记录开发框架组件时,也会出现不需要包含单独日志配置的情况。 logback的方法具有可选属性,该属性忽略文件不存在。例如, codehammer 2014-09-08 06:12:40

+0

您可能想要将此作为Log4j2 Jira问题跟踪器上的功能请求提出。 – 2014-09-08 11:27:53

+1

或者这是你在找什么? https://issues.apache.org/jira/browse/LOG4J2-494 – 2014-09-08 12:24:01

1

XInclude可以使用,但不是理想的解决方案。使用XInclude时,包含文件本身必须定义一个顶级元素,例如Appenders/Loggers/Properties。

这里是你如何使用它的一个例子:

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration status="error" xmlns:xi="http://www.w3.org/2001/XInclude"> 
    <!-- this will override the log pattern defined in the included log4j-properties.xml --> 
    <Properties> 
     <Property name="log-pattern">jit %d %-5p [%t] %C{1.} - %m%n</Property> 
    </Properties> 

    <xi:include href="log4j2-properties.xml" /> 
    <xi:include href="log4j2-appenders.xml" /> 
    <xi:include href="log4j2-loggers.xml" /> 
</Configuration> 

举个例子包括log4j2-properties.xml中可能看起来像:

<?xml version="1.0" encoding="UTF-8"?> 
<Properties> 
    <!-- define the log pattern as a property so that it can be overridden --> 
    <Property name="log-pattern">%d %-5p [%t] %C{1.} - %m%n</Property> 
</Properties> 

您可以XIncludes可选择通过使用空的“后备”块。但是,在可用的最新版本的log4j2中,这会导致出现Xerces的警告消息(因为log4j2正在使用DefaultErrorHandler)。

<xi:include href="log4j2-optional.xml"> 
    <xi:fallback /> 
</xi:include> 
相关问题