Class XmlLayout
- java.lang.Object
-
- org.apache.logging.log4j.core.layout.AbstractLayout<java.lang.String>
-
- org.apache.logging.log4j.core.layout.AbstractStringLayout
-
- org.apache.logging.log4j.core.layout.XmlLayout
-
- All Implemented Interfaces:
Layout<java.lang.String>
,Encoder<LogEvent>
,StringLayout
@Plugin(name="XmlLayout", category="Core", elementType="layout", printObject=true) public final class XmlLayout extends AbstractStringLayout
Appends a series ofevent
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 "http://logging.apache.org/log4j/2.0/events". By default, withcomplete="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 toUTF-8
orUTF-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. Ifcompact="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 Modifier and Type Class Description static class
XmlLayout.Builder<B extends XmlLayout.Builder<B>>
-
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
-
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.core.layout.AbstractJacksonLayout.ResolvableKeyValuePair[]
additionalFields
protected boolean
compact
protected static java.lang.String
COMPACT_EOL
protected boolean
complete
protected static java.lang.String
DEFAULT_EOL
protected java.lang.String
eol
protected boolean
includeNullDelimiter
protected com.fasterxml.jackson.databind.ObjectWriter
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
Constructors Modifier Constructor Description protected
XmlLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static XmlLayout
createDefaultLayout()
Creates an XML Layout using the default settings.static XmlLayout
createLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
insteadjava.util.Map<java.lang.String,java.lang.String>
getContentFormat()
Gets this XmlLayout's content format.java.lang.String
getContentType()
Returns the content type output by this layout.byte[]
getFooter()
Returns appropriate XML footer.byte[]
getHeader()
Returns appropriate XML headers.static <B extends XmlLayout.Builder<B>>
BnewBuilder()
java.lang.String
toSerializable(LogEvent event)
Formats aLogEvent
.void
toSerializable(LogEvent event, java.io.Writer writer)
protected static boolean
valueNeedsLookup(java.lang.String value)
protected java.lang.Object
wrapLogEvent(LogEvent event)
-
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getBytes, getCharset, getFooterSerializer, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
-
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
encode, getConfiguration, markEvent
-
-
-
-
Field Detail
-
DEFAULT_EOL
protected static final java.lang.String DEFAULT_EOL
- See Also:
- Constant Field Values
-
COMPACT_EOL
protected static final java.lang.String COMPACT_EOL
- See Also:
- Constant Field Values
-
eol
protected final java.lang.String 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 Detail
-
XmlLayout
@Deprecated protected XmlLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
instead
-
-
Method Detail
-
getHeader
public byte[] getHeader()
Returns appropriate XML headers.- XML processing instruction
- XML root element
- Specified by:
getHeader
in interfaceLayout<java.lang.String>
- Overrides:
getHeader
in classAbstractStringLayout
- Returns:
- a byte array containing the header.
-
getFooter
public byte[] getFooter()
Returns appropriate XML footer.- Specified by:
getFooter
in interfaceLayout<java.lang.String>
- Overrides:
getFooter
in classAbstractStringLayout
- Returns:
- a byte array containing the footer, closing the XML root element.
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> 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<java.lang.String>
- Overrides:
getContentFormat
in classAbstractLayout<java.lang.String>
- Returns:
- Map of content format keys supporting XmlLayout
-
getContentType
public java.lang.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 interfaceLayout<java.lang.String>
- Overrides:
getContentType
in classAbstractStringLayout
- Returns:
- The content type.
-
createLayout
@Deprecated public static XmlLayout createLayout(boolean locationInfo, boolean properties, boolean complete, boolean compact, java.nio.charset.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
@PluginBuilderFactory public static <B extends XmlLayout.Builder<B>> B newBuilder()
-
createDefaultLayout
public static XmlLayout createDefaultLayout()
Creates an XML Layout using the default settings.- Returns:
- an XML Layout.
-
valueNeedsLookup
protected static boolean valueNeedsLookup(java.lang.String value)
-
toSerializable
public java.lang.String toSerializable(LogEvent event)
Formats aLogEvent
.- Parameters:
event
- The LogEvent.- Returns:
- The XML representation of the LogEvent.
-
wrapLogEvent
protected java.lang.Object wrapLogEvent(LogEvent event)
-
toSerializable
public void toSerializable(LogEvent event, java.io.Writer writer) throws com.fasterxml.jackson.core.JsonGenerationException, com.fasterxml.jackson.databind.JsonMappingException, java.io.IOException
- Throws:
com.fasterxml.jackson.core.JsonGenerationException
com.fasterxml.jackson.databind.JsonMappingException
java.io.IOException
-
-