在Log4j2中,如何将XML Schema与log4j2.xml相关联?

时间:2021-06-18 21:50:53

I have been giving the new Log4j2 a go. It seems, from the documentation on migration, that the XML Schema/DTD specification has been done away with.

我一直在给新的Log4j2。从迁移文档中可以看出,XML Schema / DTD规范已被废除。

That seems like a step backwards. Surely it should be possible to associate either an XML Schema or a DTD with my log4j2.xml to assist writing it and for validation. I haven't been able to find anything useful in the documentation, and neither have I found the XML Schema or DTD itself.

这似乎是向后退一步。当然应该可以将XML Schema或DTD与我的log4j2.xml相关联,以帮助编写和验证。我无法在文档中找到任何有用的东西,也没有找到XML Schema或DTD本身。

So: In Log4j2, how should I associate an XML Schema with log4j2.xml?

那么:在Log4j2中,我应该如何将XML Schema与log4j2.xml相关联?

4 个解决方案

#1


7  

I don't think it's possible to have a schema/DTD with log4j2. Recently I've written a custom appender, and to support the appender my log4j2.xml looks like this:

我认为使用log4j2可能没有架构/ DTD。最近我写了一个自定义appender,为了支持appender我的log4j2.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR" packages="package.same.as.custom.appender">
  <appenders>
    <CyclicBuffer name="CyclicBuffer" bufferSize="200">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%t] %c{1.} - %msg%n"/>
    </CyclicBuffer>
  </appenders>
  <loggers>
    <root level="info">
      <appender-ref ref="CyclicBuffer"/>
    </root>
  </loggers>
</configuration>

The important things to note are that I've got a completely custom CyclicBuffer element, and that it has a completely custom bufferSize attribute. Have a look at the documentation surrounding @PluginFactory and @Plugin for more detail.

需要注意的重要事项是,我有一个完全自定义的CyclicBuffer元素,并且它具有完全自定义的bufferSize属性。有关更多详细信息,请查看有关@PluginFactory和@Plugin的文档。

Because of this customisation, I don't think that the XML can be validated via a standard, common XSD/DTD. Instead, I think you'll need to create your own XSD if you wish to validate the XML.

由于这种定制,我不认为XML可以通过标准的通用XSD / DTD进行验证。相反,如果您希望验证XML,我认为您需要创建自己的XSD。

One important thing to note, is that in my XML I've got: <configuration status="ERROR". When this is present log4j2 will output any errors associated with incorrect configuration at runtime. While not as convenient as XML validation, it is also very useful!

需要注意的一件重要事情是,在我的XML中我得到了:

Hope this is of some help, Muel.

Muel,希望这有一些帮助。

#2


17  

Please re-read the Log4J2 documentation about XML configuration and you'll find these 2 places:

请重新阅读有关XML配置的Log4J2文档,您将找到以下两个位置:

Log4j can be configured using two XML flavors; concise and strict. The concise format makes configuration very easy as the element names match the components they represent however it cannot be validated with an XML schema. For example, the ConsoleAppender is configured by declaring an XML element named Console under its parent appenders element. However, element and attribute names are are not case sensitive. In addition, attributes can either be specified as an XML attribute or as an XML element that has no attributes and has a text value.

可以使用两种XML风格配置Log4j;简洁严谨。简洁的格式使配置变得非常简单,因为元素名称与它们所代表的组件相匹配,但是无法使用XML模式进行验证。例如,通过在其父appenders元素下声明名为Console的XML元素来配置ConsoleAppender。但是,元素和属性名称不区分大小写。此外,属性既可以指定为XML属性,也可以指定为没有属性且具有文本值的XML元素。

and a little further:

再进一步:

Strict XML. In addition to the concise XML format above, Log4j allows configurations to be specified in a more "normal" XML manner that can be validated using an XML Schema. This is accomplished by replacing the friendly element names above with their object type as shown below. For example, instead of the ConsoleAppender being configured using an element named Console it is instead configured as an appender element with a type attribute containing "Console".

严格的XML。除了上面简洁的XML格式之外,Log4j还允许以更“正常”的XML方式指定配置,可以使用XML Schema进行验证。这是通过将上面的友好元素名称替换为其对象类型来实现的,如下所示。例如,不是使用名为Console的元素配置ConsoleAppender,而是将其配置为具有包含“Console”的type属性的appender元素。

So if you want to use XML schema validation for Log4j2 then use only Strict XML format.

因此,如果要对Log4j2使用XML模式验证,则只使用Strict XML格式。

#3


11  

I cannot seem to find a reference to the XSD on the Log4J2 website, but if you download the for 2.0 beta 5, you'll find it contains a Log4J 2 schema (Log4J-V2.0.xsd) in the path core/src/main/resources.

我似乎无法在Log4J2网站上找到对XSD的引用,但是如果你下载for for beta beta 5,你会发现它在路径core / src中包含一个Log4J 2架构(Log4J-V2.0.xsd) /主/资源。

As stated by Muel, it's quite possible to have custom appenders. Because of this I believe that it would only work if you use strict="true" in the main configuration node.

正如Muel所说,很有可能拥有自定义appender。因此我相信只有在主配置节点中使用strict =“true”才能使用它。

See: http://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax

I've used the schema attribute to add this to the configuration and it seems to mostly work.

我已经使用schema属性将其添加到配置中,它似乎主要起作用。

Here's an example:

这是一个例子:

<configuration name="testConfiguration"
    status="debug"
    strict="true"
    monitorInterval="30"
    schema="Log4J-V2.0.xsd">

I say "mostly" because one problem I've found it that even some documented valid attributes (e.g. monitorInterval) are missing from the XSD.

我说“大多数”是因为我发现有一个问题,即XSD中缺少一些记录有效的属性(例如monitorInterval)。

Hopefully with future releases an accurate XSD of the strict format will become available.

希望在将来的版本中,可以获得严格格式的准确XSD。

Hope this is of some help.

希望这个对你有帮助。

-Steve

Updated: See LOG4J2-170 regarding issue with XSD validation

更新:有关XSD验证的问题,请参阅LOG4J2-170

#4


8  

Works for me with eclipse:

适用于eclipse:

<Configuration strict="true"
           xmlns="http://logging.apache.org/log4j/2.0/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config 
           https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">

or against tagged version:

或针对标记版本:

<Configuration strict="true"
           xmlns="http://logging.apache.org/log4j/2.0/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config 
           https://raw.githubusercontent.com/apache/logging-log4j2/log4j-2.8.2/log4j-core/src/main/resources/Log4j-config.xsd">

#1


7  

I don't think it's possible to have a schema/DTD with log4j2. Recently I've written a custom appender, and to support the appender my log4j2.xml looks like this:

我认为使用log4j2可能没有架构/ DTD。最近我写了一个自定义appender,为了支持appender我的log4j2.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR" packages="package.same.as.custom.appender">
  <appenders>
    <CyclicBuffer name="CyclicBuffer" bufferSize="200">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%t] %c{1.} - %msg%n"/>
    </CyclicBuffer>
  </appenders>
  <loggers>
    <root level="info">
      <appender-ref ref="CyclicBuffer"/>
    </root>
  </loggers>
</configuration>

The important things to note are that I've got a completely custom CyclicBuffer element, and that it has a completely custom bufferSize attribute. Have a look at the documentation surrounding @PluginFactory and @Plugin for more detail.

需要注意的重要事项是,我有一个完全自定义的CyclicBuffer元素,并且它具有完全自定义的bufferSize属性。有关更多详细信息,请查看有关@PluginFactory和@Plugin的文档。

Because of this customisation, I don't think that the XML can be validated via a standard, common XSD/DTD. Instead, I think you'll need to create your own XSD if you wish to validate the XML.

由于这种定制,我不认为XML可以通过标准的通用XSD / DTD进行验证。相反,如果您希望验证XML,我认为您需要创建自己的XSD。

One important thing to note, is that in my XML I've got: <configuration status="ERROR". When this is present log4j2 will output any errors associated with incorrect configuration at runtime. While not as convenient as XML validation, it is also very useful!

需要注意的一件重要事情是,在我的XML中我得到了:

Hope this is of some help, Muel.

Muel,希望这有一些帮助。

#2


17  

Please re-read the Log4J2 documentation about XML configuration and you'll find these 2 places:

请重新阅读有关XML配置的Log4J2文档,您将找到以下两个位置:

Log4j can be configured using two XML flavors; concise and strict. The concise format makes configuration very easy as the element names match the components they represent however it cannot be validated with an XML schema. For example, the ConsoleAppender is configured by declaring an XML element named Console under its parent appenders element. However, element and attribute names are are not case sensitive. In addition, attributes can either be specified as an XML attribute or as an XML element that has no attributes and has a text value.

可以使用两种XML风格配置Log4j;简洁严谨。简洁的格式使配置变得非常简单,因为元素名称与它们所代表的组件相匹配,但是无法使用XML模式进行验证。例如,通过在其父appenders元素下声明名为Console的XML元素来配置ConsoleAppender。但是,元素和属性名称不区分大小写。此外,属性既可以指定为XML属性,也可以指定为没有属性且具有文本值的XML元素。

and a little further:

再进一步:

Strict XML. In addition to the concise XML format above, Log4j allows configurations to be specified in a more "normal" XML manner that can be validated using an XML Schema. This is accomplished by replacing the friendly element names above with their object type as shown below. For example, instead of the ConsoleAppender being configured using an element named Console it is instead configured as an appender element with a type attribute containing "Console".

严格的XML。除了上面简洁的XML格式之外,Log4j还允许以更“正常”的XML方式指定配置,可以使用XML Schema进行验证。这是通过将上面的友好元素名称替换为其对象类型来实现的,如下所示。例如,不是使用名为Console的元素配置ConsoleAppender,而是将其配置为具有包含“Console”的type属性的appender元素。

So if you want to use XML schema validation for Log4j2 then use only Strict XML format.

因此,如果要对Log4j2使用XML模式验证,则只使用Strict XML格式。

#3


11  

I cannot seem to find a reference to the XSD on the Log4J2 website, but if you download the for 2.0 beta 5, you'll find it contains a Log4J 2 schema (Log4J-V2.0.xsd) in the path core/src/main/resources.

我似乎无法在Log4J2网站上找到对XSD的引用,但是如果你下载for for beta beta 5,你会发现它在路径core / src中包含一个Log4J 2架构(Log4J-V2.0.xsd) /主/资源。

As stated by Muel, it's quite possible to have custom appenders. Because of this I believe that it would only work if you use strict="true" in the main configuration node.

正如Muel所说,很有可能拥有自定义appender。因此我相信只有在主配置节点中使用strict =“true”才能使用它。

See: http://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax

I've used the schema attribute to add this to the configuration and it seems to mostly work.

我已经使用schema属性将其添加到配置中,它似乎主要起作用。

Here's an example:

这是一个例子:

<configuration name="testConfiguration"
    status="debug"
    strict="true"
    monitorInterval="30"
    schema="Log4J-V2.0.xsd">

I say "mostly" because one problem I've found it that even some documented valid attributes (e.g. monitorInterval) are missing from the XSD.

我说“大多数”是因为我发现有一个问题,即XSD中缺少一些记录有效的属性(例如monitorInterval)。

Hopefully with future releases an accurate XSD of the strict format will become available.

希望在将来的版本中,可以获得严格格式的准确XSD。

Hope this is of some help.

希望这个对你有帮助。

-Steve

Updated: See LOG4J2-170 regarding issue with XSD validation

更新:有关XSD验证的问题,请参阅LOG4J2-170

#4


8  

Works for me with eclipse:

适用于eclipse:

<Configuration strict="true"
           xmlns="http://logging.apache.org/log4j/2.0/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config 
           https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">

or against tagged version:

或针对标记版本:

<Configuration strict="true"
           xmlns="http://logging.apache.org/log4j/2.0/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config 
           https://raw.githubusercontent.com/apache/logging-log4j2/log4j-2.8.2/log4j-core/src/main/resources/Log4j-config.xsd">