Class YamlLayout

All Implemented Interfaces:
LocationAware, Layout<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 Details

    • DEFAULT_EOL

      protected static final String DEFAULT_EOL
      See Also:
    • COMPACT_EOL

      protected static final String COMPACT_EOL
      See Also:
    • 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 Details

    • YamlLayout

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

    • getHeader

      public byte[] getHeader()
      Returns appropriate YAML header.
      Specified by:
      getHeader in interface Layout<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<String>
      Overrides:
      getFooter in class AbstractStringLayout
      Returns:
      a byte array containing the footer, closing the YAML array.
    • getContentFormat

      public Map<String,String> getContentFormat()
      Description copied from interface: Layout
      Returns a description of the content format.
      Specified by:
      getContentFormat in interface Layout<String>
      Overrides:
      getContentFormat in class AbstractLayout<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 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<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, String headerPattern, String footerPattern, 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 footer 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.
    • newBuilder

      @PluginBuilderFactory public static <B extends YamlLayout.Builder<B>> B newBuilder()
    • 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(String value)
    • toSerializable

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

      protected Object wrapLogEvent(LogEvent event)
    • toSerializable

      public void toSerializable(LogEvent event, Writer writer) throws com.fasterxml.jackson.core.JsonGenerationException, com.fasterxml.jackson.databind.JsonMappingException, IOException
      Throws:
      com.fasterxml.jackson.core.JsonGenerationException
      com.fasterxml.jackson.databind.JsonMappingException
      IOException