In Log4j2, how do I associate an XML Schema with log4j2.xml? In Log4j2, how do I associate an XML Schema with log4j2.xml? java java

In Log4j2, how do I associate an XML Schema with log4j2.xml?


Works for me with 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">


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

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.

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".

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


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.

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.

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.

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.

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

Hope this is of some help.

-Steve

Updated:See LOG4J2-170 regarding issue with XSD validation