log4j2文件包含:<include>和<uncrese>类似于logback[英] Log4j2 File Inclusion : <include> and <included> similar to Logback

本文是小编为大家收集整理的关于log4j2文件包含:<include>和<uncrese>类似于logback的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

log4j2像logback一样支持文件包含机制吗?这是为了在另一个文件(包含附录,登录器等)中包含配置文件的一部分.

作为fyi-这是它在记录中的工作方式:

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

示例:文件include(logback-examples/src/main/java/java/章节/configuration/containingconfig.xml)

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

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

目标文件必须将其元素嵌套在元素内.例如,可以将consoleappender声明为:

示例:文件include(logback-examples/src/main/java/java/章节/configuration/uncludnconfig.xml)

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

推荐答案

可以使用Xinclude,但不是理想的解决方案.当使用Xinclude时,Include文件本身必须定义单个顶级元素,例如附录/记录器/属性.

这是您如何使用它的一个示例:

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

其他推荐答案

在log4j2中存在略有不同但相似的机制,它支持Xinclude. 请参阅 a>以获取详细信息. (这需要更好地记录...)

编辑:文档在这里: https://logging.apache.apache.org.orggace.org/logg/log4j/2. X/Manual/configuration.html#xinclude

本文地址:https://www.itbaoku.cn/post/1574837.html

问题描述

Does Log4j2 support file inclusion mechanism like Logback does ? This is for including parts of a configuration file from another file (containing appenders, loggers etc.)

As an FYI - Here is how it works in Logback:

Joran supports including parts of a configuration file from another file. This is done by declaring a element, as shown below:

Example: File include (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:

Example: File include (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. When using XInclude the include files themselves have to define a single top level element such as 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:

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

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

其他推荐答案

A slightly different but similar mechanism exists in Log4j2, it supports XInclude. See https://issues.apache.org/jira/browse/LOG4J2-341 for details. (This needs to be documented better...)

Edit: the docs are here: https://logging.apache.org/log4j/2.x/manual/configuration.html#XInclude