Class JsonLayout
- 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.JsonLayout
-
- All Implemented Interfaces:
Layout<java.lang.String>
,Encoder<LogEvent>
,StringLayout
@Plugin(name="JsonLayout", category="Core", elementType="layout", printObject=true) public final class JsonLayout extends AbstractStringLayout
Appends a series of JSON events as strings serialized as bytes.Complete well-formed JSON vs. fragment JSON
If you configure
complete="true"
, the appender outputs a well-formed JSON document. By default, withcomplete="false"
, you should include the output as an external file in a separate file to form a well-formed JSON document.If
complete="false"
, the appender does not write the JSON open array character "[" at the start of the document, "]" and the end, nor comma "," between records.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 JSON
By default, the JSON layout is not compact (a.k.a. "pretty") with
compact="false"
, which means the appender uses end-of-line characters and indents lines to format the text. Ifcompact="true"
, then no end-of-line or indentation is used. Message content may contain, of course, escaped end-of-lines.Additional Fields
This property allows addition of custom fields into generated JSON.
<JsonLayout><KeyValuePair key="foo" value="bar"/></JsonLayout>
inserts"foo":"bar"
directly into JSON output. Supports Lookup expressions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsonLayout.Builder<B extends JsonLayout.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
JsonLayout(Configuration config, boolean locationInfo, boolean properties, boolean encodeThreadContextAsList, boolean complete, boolean compact, boolean eventEol, java.lang.String headerPattern, java.lang.String footerPattern, 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 JsonLayout
createDefaultLayout()
Creates a JSON Layout using the default settings.static JsonLayout
createLayout(Configuration config, boolean locationInfo, boolean properties, boolean propertiesAsList, boolean complete, boolean compact, boolean eventEol, java.lang.String headerPattern, java.lang.String footerPattern, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
insteadjava.util.Map<java.lang.String,java.lang.String>
getContentFormat()
Returns a description of the content format.java.lang.String
getContentType()
Returns the content type output by this layout.byte[]
getFooter()
Returns appropriate JSON footer.byte[]
getHeader()
Returns appropriate JSON header.static <B extends JsonLayout.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
-
JsonLayout
@Deprecated protected JsonLayout(Configuration config, boolean locationInfo, boolean properties, boolean encodeThreadContextAsList, boolean complete, boolean compact, boolean eventEol, java.lang.String headerPattern, java.lang.String footerPattern, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
instead
-
-
Method Detail
-
getHeader
public byte[] getHeader()
Returns appropriate JSON header.- Specified by:
getHeader
in interfaceLayout<java.lang.String>
- Overrides:
getHeader
in classAbstractStringLayout
- Returns:
- a byte array containing the header, opening the JSON array.
-
getFooter
public byte[] getFooter()
Returns appropriate JSON footer.- Specified by:
getFooter
in interfaceLayout<java.lang.String>
- Overrides:
getFooter
in classAbstractStringLayout
- Returns:
- a byte array containing the footer, closing the JSON array.
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
Description copied from interface:Layout
Returns a description of the content format.- Specified by:
getContentFormat
in interfaceLayout<java.lang.String>
- Overrides:
getContentFormat
in classAbstractLayout<java.lang.String>
- Returns:
- a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified.
-
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 JsonLayout createLayout(Configuration config, boolean locationInfo, boolean properties, boolean propertiesAsList, boolean complete, boolean compact, boolean eventEol, java.lang.String headerPattern, java.lang.String footerPattern, java.nio.charset.Charset charset, boolean includeStacktrace)
Deprecated.UsenewBuilder()
insteadCreates a JSON Layout.- Parameters:
config
- The plugin configuration.locationInfo
- If "true", includes the location information in the generated JSON.properties
- If "true", includes the thread context map in the generated JSON.propertiesAsList
- If true, the thread context map is included as a list of map entry objects, where each entry has a "key" attribute (whose value is the key) and a "value" attribute (whose value is the value). Defaults to false, in which case the thread context map is included as a simple map of key-value pairs.complete
- If "true", includes the JSON header and footer, and comma between records.compact
- If "true", does not use end-of-lines and indentation, defaults to "false".eventEol
- If "true", forces an EOL after each log event (even if compact is "true"), defaults to "false". This allows one even per line, even in compact mode.headerPattern
- The header pattern, defaults to"["
if null.footerPattern
- The header pattern, defaults to"]"
if null.charset
- The character set to use, ifnull
, uses "UTF-8".includeStacktrace
- If "true", includes the stacktrace of any Throwable in the generated JSON, defaults to "true".- Returns:
- A JSON Layout.
-
newBuilder
@PluginBuilderFactory public static <B extends JsonLayout.Builder<B>> B newBuilder()
-
createDefaultLayout
public static JsonLayout createDefaultLayout()
Creates a JSON Layout using the default settings. Useful for testing.- Returns:
- A JSON Layout.
-
toSerializable
public void toSerializable(LogEvent event, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
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)
-
-