Class XmlLayout

  • All Implemented Interfaces:
    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 "http://logging.apache.org/log4j/2.0/events". 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.

    Encoding

    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 Detail

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

      • XmlLayout

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

      • getHeader

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

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

        @Deprecated
        public static XmlLayout createLayout​(boolean locationInfo,
                                             boolean properties,
                                             boolean complete,
                                             boolean compact,
                                             Charset charset,
                                             boolean includeStacktrace)
        Deprecated.
        Use newBuilder() instead
        Creates an XML Layout.
        Parameters:
        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".
        Returns:
        An XML Layout.
      • createDefaultLayout

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

        protected static boolean valueNeedsLookup​(String value)
      • toSerializable

        public String toSerializable​(LogEvent event)
        Formats a LogEvent.
        Parameters:
        event - The LogEvent.
        Returns:
        The XML representation of the LogEvent.
      • toSerializable

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