public class XMLLayout extends AbstractStringLayout
event
elements as defined in the log4j.dtd.
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.
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.
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.
footer, header, LOGGER
Modifier | Constructor and Description |
---|---|
protected |
XMLLayout(boolean locationInfo,
boolean properties,
boolean complete,
boolean compact,
String nsPrefix,
Charset charset) |
Modifier and Type | Method and Description |
---|---|
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. |
getCharset, toByteArray
setFooter, setHeader
public String toSerializable(LogEvent event)
LogEvent
in conformance with the log4j.dtd.event
- The LogEvent.public byte[] getHeader()
public byte[] getFooter()
public Map<String,String> getContentFormat()
public String getContentType()
Layout
getContentType
in interface Layout<String>
getContentType
in class AbstractStringLayout
public static XMLLayout createLayout(String locationInfo, String properties, String completeStr, String compactStr, String namespacePrefix, String charsetName)
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".Copyright © 1999-2014 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.