Question

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.

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.

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

Was it helpful?

Solution 4

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:

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

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.

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!

Hope this is of some help, Muel.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top