javalog4jlog4j2

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


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?


Solution

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