Class YamlLayout

  • All Implemented Interfaces:
    Layout<java.lang.String>, Encoder<LogEvent>, StringLayout

    @Plugin(name="YamlLayout",
            category="Core",
            elementType="layout",
            printObject=true)
    public final class YamlLayout
    extends AbstractStringLayout
    Appends a series of YAML events as strings serialized as bytes.

    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.

    Additional Fields

    This property allows addition of custom fields into generated JSON. <YamlLayout><KeyValuePair key="foo" value="bar"/></YamlLayout> inserts foo: "bar" directly into YAML output. Supports Lookup expressions.

    • Field Detail

      • 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

      • YamlLayout

        @Deprecated
        protected YamlLayout​(Configuration config,
                             boolean locationInfo,
                             boolean properties,
                             boolean complete,
                             boolean compact,
                             boolean eventEol,
                             java.lang.String headerPattern,
                             java.lang.String footerPattern,
                             java.nio.charset.Charset charset,
                             boolean includeStacktrace)
        Deprecated.
        Use newBuilder() instead
    • Method Detail

      • getHeader

        public byte[] getHeader()
        Returns appropriate YAML header.
        Specified by:
        getHeader in interface Layout<java.lang.String>
        Overrides:
        getHeader in class AbstractStringLayout
        Returns:
        a byte array containing the header, opening the YAML array.
      • getFooter

        public byte[] getFooter()
        Returns appropriate YAML footer.
        Specified by:
        getFooter in interface Layout<java.lang.String>
        Overrides:
        getFooter in class AbstractStringLayout
        Returns:
        a byte array containing the footer, closing the YAML 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 interface Layout<java.lang.String>
        Overrides:
        getContentFormat in class AbstractLayout<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 interface Layout<java.lang.String>
        Overrides:
        getContentType in class AbstractStringLayout
        Returns:
        The content type.
      • createLayout

        @Deprecated
        public static org.apache.logging.log4j.core.layout.AbstractJacksonLayout createLayout​(Configuration config,
                                                                                              boolean locationInfo,
                                                                                              boolean properties,
                                                                                              java.lang.String headerPattern,
                                                                                              java.lang.String footerPattern,
                                                                                              java.nio.charset.Charset charset,
                                                                                              boolean includeStacktrace)
        Deprecated.
        Use newBuilder() instead
        Creates a YAML Layout.
        Parameters:
        config - The plugin configuration.
        locationInfo - If "true", includes the location information in the generated YAML.
        properties - If "true", includes the thread context map in the generated YAML.
        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 YAML, defaults to "true".
        Returns:
        A YAML Layout.
      • createDefaultLayout

        public static org.apache.logging.log4j.core.layout.AbstractJacksonLayout createDefaultLayout()
        Creates a YAML Layout using the default settings. Useful for testing.
        Returns:
        A YAML Layout.
      • valueNeedsLookup

        protected static boolean valueNeedsLookup​(java.lang.String value)
      • toSerializable

        public java.lang.String toSerializable​(LogEvent event)
        Formats a LogEvent.
        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