Class XmlLayout

All Implemented Interfaces:
LocationAware, Layout<String>, Encoder<LogEvent>, StringLayout

@Plugin(name="XmlLayout", category="Core", elementType="layout", printObject=true) public final class XmlLayout extends AbstractStringLayout
Appends a series of event elements as defined in the log4j.dtd.

Complete well-formed XML vs. fragment XML

If you configure complete="true", the appender outputs a well-formed XML document where the default namespace is the log4j namespace "". By default, with complete="false", you should include the output as an external entity in a separate file to form a well-formed XML document.

If complete="false", the appender does not write the XML processing instruction and the root element.


Appenders using this layout should have their charset set to UTF-8 or UTF-16, otherwise events containing non-ASCII characters could result in corrupted log files.

Pretty vs. compact XML

By default, the XML layout is not compact (compact = not "pretty") with compact="false", which means the appender uses end-of-line characters and indents lines to format the XML. If compact="true", then no end-of-line or indentation is used. Message content may contain, of course, end-of-lines.

Additional Fields

This property allows addition of custom fields into generated JSON. <XmlLayout><KeyValuePair key="foo" value="bar"/></XmlLayout> inserts <foo>bar</foo> directly into XML output. Supports Lookup expressions.

  • Field Details


      protected static final String DEFAULT_EOL
      See Also:

      protected static final String COMPACT_EOL
      See Also:
    • eol

      protected final String eol
    • objectWriter

      protected final com.fasterxml.jackson.databind.ObjectWriter objectWriter
    • compact

      protected final boolean compact
    • complete

      protected final boolean complete
    • includeNullDelimiter

      protected final boolean includeNullDelimiter
    • additionalFields

      protected final org.apache.logging.log4j.core.layout.AbstractJacksonLayout.ResolvableKeyValuePair[] additionalFields
  • Constructor Details

    • XmlLayout

      @Deprecated protected XmlLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset, boolean includeStacktrace)
      Use newBuilder() instead
  • Method Details

    • getHeader

      public byte[] getHeader()
      Returns appropriate XML headers.
      1. XML processing instruction
      2. XML root element
      Specified by:
      getHeader in interface Layout<String>
      getHeader in class AbstractStringLayout
      a byte array containing the header.
    • getFooter

      public byte[] getFooter()
      Returns appropriate XML footer.
      Specified by:
      getFooter in interface Layout<String>
      getFooter in class AbstractStringLayout
      a byte array containing the footer, closing the XML root element.
    • getContentFormat

      public Map<String,String> getContentFormat()
      Gets this XmlLayout's content format. Specified by:
      • Key: "dtd" Value: "log4j-events.dtd"
      • Key: "version" Value: "2.0"
      Specified by:
      getContentFormat in interface Layout<String>
      getContentFormat in class AbstractLayout<String>
      Map of content format keys supporting XmlLayout
    • getContentType

      public String getContentType()
      Description copied from interface: Layout
      Returns the content type output by this layout. The base class returns "text/plain".
      Specified by:
      getContentType in interface Layout<String>
      getContentType in class AbstractStringLayout
      The content type.
    • createLayout

      @Deprecated public static XmlLayout createLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset, boolean includeStacktrace)
      Use newBuilder() instead
      Creates an XML Layout.
      locationInfo - If "true", includes the location information in the generated XML.
      properties - If "true", includes the thread context map in the generated XML.
      complete - If "true", includes the XML header and footer, defaults to "false".
      compact - If "true", does not use end-of-lines and indentation, defaults to "false".
      charset - The character set to use, if null, uses "UTF-8".
      includeStacktrace - If "true", includes the stacktrace of any Throwable in the generated XML, defaults to "true".
      An XML Layout.
    • newBuilder

      @PluginBuilderFactory public static <B extends XmlLayout.Builder<B>> B newBuilder()
    • createDefaultLayout

      public static XmlLayout createDefaultLayout()
      Creates an XML Layout using the default settings.
      an XML Layout.
    • valueNeedsLookup

      protected static boolean valueNeedsLookup(String value)
    • toSerializable

      public String toSerializable(LogEvent event)
      Formats a LogEvent.
      event - The LogEvent.
      The XML representation of the LogEvent.
    • wrapLogEvent

      protected Object wrapLogEvent(LogEvent event)
    • toSerializable

      public void toSerializable(LogEvent event, Writer writer) throws com.fasterxml.jackson.core.JsonGenerationException, com.fasterxml.jackson.databind.JsonMappingException, IOException