简体   繁体   English

Log4j2文件包含:<include>和<included>类似于Logback

[英]Log4j2 File Inclusion : <include> and <included> similar to Logback

Does Log4j2 support file inclusion mechanism like Logback does ? Log4j2是否支持像Logback这样的文件包含机制? This is for including parts of a configuration file from another file (containing appenders, loggers etc.) 这是为了包含来自另一个文件的配置文件的一部分(包含appender,loggers等)

As an FYI - Here is how it works in Logback: 作为FYI - 以下是它在Logback中的工作原理:

Joran supports including parts of a configuration file from another file. Joran支持从另一个文件中包含部分配置文件。 This is done by declaring a element, as shown below: 这是通过声明一个元素来完成的,如下所示:

Example: File include (logback-examples/src/main/java/chapters/configuration/containingConfig.xml) 示例:文件包含(logback-examples / src / main / java / chapters / configuration / containingConfig.xml)

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

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

The target file MUST have its elements nested inside an element. 目标文件必须将其元素嵌套在元素中。 For example, a ConsoleAppender could be declared as: 例如,ConsoleAppender可以声明为:

Example: File include (logback-examples/src/main/java/chapters/configuration/includedConfig.xml) 示例:文件包含(logback-examples / src / main / java / chapters / configuration / includedConfig.xml)

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

XInclude can be used, but isn't an ideal solution. 可以使用XInclude,但不是理想的解决方案。 When using XInclude the include files themselves have to define a single top level element such as Appenders/Loggers/Properties. 使用XInclude时,包含文件本身必须定义单个顶级元素,例如Appenders / Loggers / Properties。

Here's an example of how you could use it: 以下是如何使用它的示例:

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

As an example include the log4j2-properties.xml could look like: 作为示例,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>

You can make XIncludes optional by using an empty "fallback" block. 您可以使用空的“后备”块使XIncludes成为可选项。 However in the latest version of log4j2 that is available this results in a warning message coming out of Xerces (since the DefaultErrorHandler is being used by log4j2). 但是,在最新版本的log4j2中,这会产生一条来自Xerces的警告消息(因为log4j2正在使用DefaultErrorHandler)。

<xi:include href="log4j2-optional.xml">
    <xi:fallback />
</xi:include>

A slightly different but similar mechanism exists in Log4j2, it supports XInclude. Log4j2中存在一种略有不同但相似的机制,它支持XInclude。 See https://issues.apache.org/jira/browse/LOG4J2-341 for details. 有关详细信息,请参阅https://issues.apache.org/jira/browse/LOG4J2-341 (This needs to be documented better...) (这需要更好地记录......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM