cod*_*mer 7 java configuration logging log4j log4j2
Log4j2 是否支持像 Logback 那样的文件包含机制?这是为了包含来自另一个文件的配置文件的一部分(包含附加程序、记录器等)
仅供参考 - 以下是它在 Logback 中的工作方式:
Joran 支持从另一个文件中包含部分配置文件。这是通过声明一个元素来完成的,如下所示:
示例:文件包含(logback-examples/src/main/java/chapters/configuration/ containsConfig.xml)
<configuration>
<include file="src/main/java/chapters/configuration/includedConfig.xml"/>
<root level="DEBUG">
<appender-ref ref="includedConsole" />
</root>
Run Code Online (Sandbox Code Playgroud)
目标文件必须将其元素嵌套在一个元素内。例如,可以将 ConsoleAppender 声明为:
示例:文件包含(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>
Run Code Online (Sandbox Code Playgroud)
可以使用 XIndus,但不是理想的解决方案。使用 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>
Run Code Online (Sandbox Code Playgroud)
作为示例,包含 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>
Run Code Online (Sandbox Code Playgroud)
您可以通过使用空的“后备”块将 XIncludes 设为可选。然而,在最新版本的 log4j2 中,这会导致 Xerces 发出警告消息(因为 log4j2 使用了 DefaultErrorHandler)。
<xi:include href="log4j2-optional.xml">
<xi:fallback />
</xi:include>
Run Code Online (Sandbox Code Playgroud)
Log4j2 中存在略有不同但相似的机制,它支持 XInclude。有关详细信息,请参阅https://issues.apache.org/jira/browse/LOG4J2-341。(这需要更好地记录下来......)
编辑:文档在这里: https ://logging.apache.org/log4j/2.x/manual/configuration.html#XInclude
| 归档时间: |
|
| 查看次数: |
5288 次 |
| 最近记录: |