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

The output of the XMLLayout consists of a series of log4j:event elements as defined in the log4j.dtd. If configured to do so it will output a complete well-formed XML file. The output is designed to be included as an external entity in a separate file to form a correct XML file.

For example, if abc is the name of the file where the XMLLayout ouput goes, then a well-formed XML file would be:

 <?xml version="1.0" ?>

 <!DOCTYPE log4j:eventSet SYSTEM "log4j.dtd" [<!ENTITY data SYSTEM "abc">]>

 <log4j:eventSet version="1.2" xmlns:log4j="http://logging.apache.org/log4j/">
   &data;
 </log4j:eventSet>
 

This approach enforces the independence of the XMLLayout and the appender where it is embedded.

The version attribute helps components to correctly interpret output generated by XMLLayout. The value of this attribute should be "1.1" for output generated by log4j versions prior to log4j 1.2 (final release) and "1.2" for release 1.2 and later.

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


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, Charset charset)
           
 
Method Summary
static XMLLayout createLayout(String locationInfo, String properties, String complete, String charsetName)
          Create an XML Layout.
 Map<String,String> getContentFormat()
          XMLLayout's content format is specified by:

Key: "dtd" Value: "log4j.dtd"

Key: "version" Value: "1.2"

 String getContentType()
          Returns the content type output by this layout.
 byte[] getFooter()
          Returns appropriate XML headers.
 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,
                    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.

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

Specified by:
getFooter in interface Layout<String>
Overrides:
getFooter in class AbstractLayout<String>
Returns:
a byte array containing the footer.

getContentFormat

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

Key: "dtd" Value: "log4j.dtd"

Key: "version" Value: "1.2"

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 complete,
                                     String charsetName)
Create an XML Layout.

Parameters:
locationInfo - If "true" include the location information in the generated XML.
properties - If "true" include the thread context in the generated XML.
complete - If "true" include the XML header.
charsetName - The character set to use.
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.