Class XmlLayout
- All Implemented Interfaces:
LocationAware
,Layout<String>
,Encoder<LogEvent>
,StringLayout
event
elements as defined in the
Log4j-events.xsd.
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
-
Field Summary
Modifier and TypeFieldDescriptionprotected final org.apache.logging.log4j.core.layout.AbstractJacksonLayout.ResolvableKeyValuePair[]
protected final boolean
protected static final String
protected final boolean
protected static final String
protected final String
protected final boolean
protected final com.fasterxml.jackson.databind.ObjectWriter
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
DEFAULT_STRING_BUILDER_SIZE, MAX_STRING_BUILDER_SIZE
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
configuration, eventCount, footer, header, LOGGER
Fields inherited from interface org.apache.logging.log4j.core.Layout
ELEMENT_TYPE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic XmlLayout
Creates an XML Layout using the default settings.static XmlLayout
createLayout
(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset, boolean includeStacktrace) Deprecated.UsenewBuilder()
insteadGets this XmlLayout's content format.Returns the content type output by this layout.byte[]
Returns appropriate XML footer.byte[]
Returns appropriate XML headers.static <B extends XmlLayout.Builder<B>>
BtoSerializable
(LogEvent event) Formats aLogEvent
.void
toSerializable
(LogEvent event, Writer writer) protected static boolean
valueNeedsLookup
(String value) protected Object
wrapLogEvent
(LogEvent event) Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getBytes, getCharset, getFooterSerializer, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, requiresLocation, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
encode, getConfiguration, markEvent
-
Field Details
-
DEFAULT_EOL
- See Also:
-
COMPACT_EOL
- See Also:
-
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 Details
-
XmlLayout
@Deprecated protected XmlLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset, boolean includeStacktrace) Deprecated.UsenewBuilder()
instead
-
-
Method Details
-
getHeader
public byte[] getHeader()Returns appropriate XML headers.- XML processing instruction
- XML root element
- Specified by:
getHeader
in interfaceLayout<String>
- Overrides:
getHeader
in classAbstractStringLayout
- Returns:
- a byte array containing the header.
-
getContentFormat
Gets this XmlLayout's content format. Specified by:- Key: "dtd" Value: "log4j-events.dtd"
- Key: "version" Value: "2.0"
- Specified by:
getContentFormat
in interfaceLayout<String>
- Overrides:
getContentFormat
in classAbstractLayout<String>
- Returns:
- Map of content format keys supporting XmlLayout
-
getContentType
Description copied from interface:Layout
Returns the content type output by this layout. The base class returns "text/plain".- Specified by:
getContentType
in interfaceLayout<String>
- Overrides:
getContentType
in classAbstractStringLayout
- Returns:
- The content type.
-
createLayout
@Deprecated public static XmlLayout createLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, Charset charset, boolean includeStacktrace) Deprecated.UsenewBuilder()
insteadCreates 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, ifnull
, uses "UTF-8".includeStacktrace
- If "true", includes the stacktrace of any Throwable in the generated XML, defaults to "true".- Returns:
- An XML Layout.
-
newBuilder
-
createDefaultLayout
Creates an XML Layout using the default settings.- Returns:
- an XML Layout.
-
valueNeedsLookup
-
toSerializable
Formats aLogEvent
.- Parameters:
event
- The LogEvent.- Returns:
- The XML representation of the LogEvent.
-
wrapLogEvent
-
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
-
newBuilder()
instead