Layout<String>
, Encoder<LogEvent>
, StringLayout
@Plugin(name="JsonLayout", category="Core", elementType="layout", printObject=true) public final class JsonLayout extends AbstractStringLayout
If you configure complete="true"
, the appender outputs a well-formed JSON document. By default, with
complete="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.
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 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. If compact="true"
, then no
end-of-line or indentation is used. Message content may contain, of course, escaped end-of-lines.
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.
Modifier and Type | Class | Description |
---|---|---|
static class |
JsonLayout.Builder<B extends JsonLayout.Builder<B>> |
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
Modifier and Type | Field | Description |
---|---|---|
protected org.apache.logging.log4j.core.layout.AbstractJacksonLayout.ResolvableKeyValuePair[] |
additionalFields |
|
protected boolean |
compact |
|
protected static String |
COMPACT_EOL |
|
protected boolean |
complete |
|
protected static String |
DEFAULT_EOL |
|
protected String |
eol |
|
protected boolean |
includeNullDelimiter |
|
protected com.fasterxml.jackson.databind.ObjectWriter |
objectWriter |
configuration, eventCount, footer, header, LOGGER
DEFAULT_STRING_BUILDER_SIZE, MAX_STRING_BUILDER_SIZE
ELEMENT_TYPE
Modifier | Constructor | Description |
---|---|---|
protected |
JsonLayout(Configuration config,
boolean locationInfo,
boolean properties,
boolean encodeThreadContextAsList,
boolean complete,
boolean compact,
boolean eventEol,
String headerPattern,
String footerPattern,
Charset charset,
boolean includeStacktrace) |
Deprecated.
Use
newBuilder() instead |
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,
String headerPattern,
String footerPattern,
Charset charset,
boolean includeStacktrace) |
Deprecated.
Use
newBuilder() instead |
Map<String,String> |
getContentFormat() |
Returns a description of the content format.
|
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>> |
newBuilder() |
|
String |
toSerializable(LogEvent event) |
Formats a
LogEvent . |
void |
toSerializable(LogEvent event,
Writer writer) |
|
protected static boolean |
valueNeedsLookup(String value) |
|
protected Object |
wrapLogEvent(LogEvent event) |
encode, getConfiguration, markEvent
getBytes, getCharset, getFooterSerializer, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
protected static final String DEFAULT_EOL
protected static final String COMPACT_EOL
protected final String eol
protected final com.fasterxml.jackson.databind.ObjectWriter objectWriter
protected final boolean compact
protected final boolean complete
protected final boolean includeNullDelimiter
protected final org.apache.logging.log4j.core.layout.AbstractJacksonLayout.ResolvableKeyValuePair[] additionalFields
@Deprecated protected JsonLayout(Configuration config, boolean locationInfo, boolean properties, boolean encodeThreadContextAsList, boolean complete, boolean compact, boolean eventEol, String headerPattern, String footerPattern, Charset charset, boolean includeStacktrace)
newBuilder()
insteadpublic byte[] getHeader()
getHeader
in interface Layout<String>
getHeader
in class AbstractStringLayout
public byte[] getFooter()
getFooter
in interface Layout<String>
getFooter
in class AbstractStringLayout
public Map<String,String> getContentFormat()
Layout
getContentFormat
in interface Layout<String>
getContentFormat
in class AbstractLayout<String>
public String getContentType()
Layout
getContentType
in interface Layout<String>
getContentType
in class AbstractStringLayout
@Deprecated public static JsonLayout createLayout(Configuration config, boolean locationInfo, boolean properties, boolean propertiesAsList, boolean complete, boolean compact, boolean eventEol, String headerPattern, String footerPattern, Charset charset, boolean includeStacktrace)
newBuilder()
insteadconfig
- 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, if null
, uses "UTF-8".includeStacktrace
- If "true", includes the stacktrace of any Throwable in the generated JSON, defaults to "true".@PluginBuilderFactory public static <B extends JsonLayout.Builder<B>> B newBuilder()
public static JsonLayout createDefaultLayout()
public void toSerializable(LogEvent event, Writer writer) throws IOException
IOException
protected static boolean valueNeedsLookup(String value)
public String toSerializable(LogEvent event)
LogEvent
.event
- The LogEvent.Copyright © 1999-2018. 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.