Class JsonLayout

  • All Implemented Interfaces:
    Layout<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, 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.

    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 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. If compact="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.

    • Field Detail

      • eol

        protected final 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,
                             String headerPattern,
                             String footerPattern,
                             Charset charset,
                             boolean includeStacktrace)
        Deprecated.
        Use newBuilder() instead
    • Method Detail

      • createLayout

        @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)
        Deprecated.
        Use newBuilder() instead
        Creates 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, if null, uses "UTF-8".
        includeStacktrace - If "true", includes the stacktrace of any Throwable in the generated JSON, defaults to "true".
        Returns:
        A JSON Layout.
      • createDefaultLayout

        public static JsonLayout createDefaultLayout()
        Creates a JSON Layout using the default settings. Useful for testing.
        Returns:
        A JSON Layout.
      • valueNeedsLookup

        protected static boolean valueNeedsLookup​(String value)
      • toSerializable

        public String toSerializable​(LogEvent event)
        Formats a LogEvent.
        Parameters:
        event - The LogEvent.
        Returns:
        The XML representation of the LogEvent.