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

java.lang.Object
  extended by org.apache.logging.log4j.core.layout.LayoutBase<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 intrepret 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 relase 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.LayoutBase
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 charset)
          Create an XML Layout.
 String formatAs(LogEvent event)
          Formats a LogEvent in conformance with the log4j.dtd.
 byte[] getFooter()
          Returns appropriate XML headers.
 byte[] getHeader()
          Returns appropriate XML headers.
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
format, getCharset
 
Methods inherited from class org.apache.logging.log4j.core.layout.LayoutBase
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

formatAs

public String formatAs(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 LayoutBase<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 LayoutBase<String>
Returns:
a byte array containing the footer.

createLayout

public static XMLLayout createLayout(String locationInfo,
                                     String properties,
                                     String complete,
                                     String charset)
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.
charset - The character set to use.
Returns:
An XML Layout.


Copyright © 1999-2012 Apache Software Foundation. All Rights Reserved.