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

glts picture glts · Dec 16, 2012 · Viewed 24.7k times · Source

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?

Answer

glory1 picture glory1 · Oct 16, 2013

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.