org.apache.logging.log4j.core.layout
Class XMLLayout

java.lang.Object
  extended by org.apache.logging.log4j.core.layout.AbstractLayout<String>
      extended by org.apache.logging.log4j.core.layout.AbstractStringLayout
          extended by org.apache.logging.log4j.core.layout.XMLLayout
All Implemented Interfaces:
Layout<String>

public 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, in which case the appender uses namespacePrefix with a default of .

A well-formed XML document follows this pattern:

 <?xml version="1.0" encoding="UTF-8"?>
 <Events xmlns="http://logging.apache.org/log4j/2.0/events">
   <Event logger="com.foo.Bar" timestamp="1373436580419" level="INFO" thread="main">
     <Message><![CDATA[This is a log message 1]]></Message>
   </Event>
   <Event logger="com.foo.Baz" timestamp="1373436580420" level="INFO" thread="main">
     <Message><![CDATA[This is a log message 2]]></Message>
   </Event>
 </Events>
 

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

This approach enforces the independence of the XMLLayout and the appender where you embed it.

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 (a.k.a. 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.


Field Summary
 
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
footer, header, LOGGER
 
Constructor Summary
protected XMLLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, String nsPrefix, Charset charset)
           
 
Method Summary
static XMLLayout createLayout(String locationInfo, String properties, String completeStr, String compactStr, String namespacePrefix, String charsetName)
          Creates an XML Layout.
 Map<String,String> getContentFormat()
          XMLLayout's content format is specified by:

Key: "dtd" Value: "log4j-events.dtd"

Key: "version" Value: "2.0"

 String getContentType()
          Returns the content type output by this layout.
 byte[] getFooter()
          Returns appropriate XML footer.
 byte[] getHeader()
          Returns appropriate XML headers.
 String toSerializable(LogEvent event)
          Formats a LogEvent in conformance with the log4j.dtd.
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getCharset, toByteArray
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
setFooter, setHeader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLLayout

protected XMLLayout(boolean locationInfo,
                    boolean properties,
                    boolean complete,
                    boolean compact,
                    String nsPrefix,
                    Charset charset)
Method Detail

toSerializable

public String toSerializable(LogEvent event)
Formats a LogEvent in conformance with the log4j.dtd.

Parameters:
event - The LogEvent.
Returns:
The XML representation of the LogEvent.

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 AbstractLayout<String>
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 AbstractLayout<String>
Returns:
a byte array containing the footer, closing the XML root element.

getContentFormat

public Map<String,String> getContentFormat()
XMLLayout's content format is specified by:

Key: "dtd" Value: "log4j-events.dtd"

Key: "version" Value: "2.0"

Returns:
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>
Overrides:
getContentType in class AbstractStringLayout
Returns:
The default content type for Strings.

createLayout

public static XMLLayout createLayout(String locationInfo,
                                     String properties,
                                     String completeStr,
                                     String compactStr,
                                     String namespacePrefix,
                                     String charsetName)
Creates an XML Layout.

Parameters:
locationInfo - If "true", includes the location information in the generated XML.
properties - If "true", includes the thread context in the generated XML.
completeStr - If "true", includes the XML header and footer, defaults to "false".
compactStr - If "true", does not use end-of-lines and indentation, defaults to "false".
namespacePrefix - The namespace prefix, defaults to
charsetName - The character set to use, if null, uses "UTF-8".
Returns:
An XML Layout.


Copyright © 1999-2013 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.