Class AbstractStringLayout

java.lang.Object
org.apache.logging.log4j.core.layout.AbstractLayout<String>
org.apache.logging.log4j.core.layout.AbstractStringLayout
All Implemented Interfaces:
LocationAware, Layout<String>, Encoder<LogEvent>, StringLayout
Direct Known Subclasses:
AbstractCsvLayout, GelfLayout, HtmlLayout, JsonLayout, PatternLayout, Rfc5424Layout, SyslogLayout, XmlLayout, YamlLayout

public abstract class AbstractStringLayout extends AbstractLayout<String> implements StringLayout, LocationAware
Abstract base class for Layouts that result in a String.

Since 2.4.1, this class has custom logic to convert ISO-8859-1 or US-ASCII Strings to byte[] arrays to improve performance: all characters are simply cast to bytes.

  • Field Details

    • DEFAULT_STRING_BUILDER_SIZE

      protected static final int DEFAULT_STRING_BUILDER_SIZE
      Default length for new StringBuilder instances: 1024 .
      See Also:
    • MAX_STRING_BUILDER_SIZE

      protected static final int MAX_STRING_BUILDER_SIZE
  • Constructor Details

    • AbstractStringLayout

      protected AbstractStringLayout(Charset charset)
    • AbstractStringLayout

      protected AbstractStringLayout(Charset aCharset, byte[] header, byte[] footer)
      Builds a new layout.
      Parameters:
      aCharset - the charset used to encode the header bytes, footer bytes and anything else that needs to be converted from strings to bytes.
      header - the header bytes
      footer - the footer bytes
    • AbstractStringLayout

      protected AbstractStringLayout(Configuration config, Charset aCharset, AbstractStringLayout.Serializer headerSerializer, AbstractStringLayout.Serializer footerSerializer)
      Builds a new layout.
      Parameters:
      config - the configuration. May be null.
      aCharset - the charset used to encode the header bytes, footer bytes and anything else that needs to be converted from strings to bytes.
      headerSerializer - the header bytes serializer
      footerSerializer - the footer bytes serializer
  • Method Details

    • requiresLocation

      public boolean requiresLocation()
      Specified by:
      requiresLocation in interface LocationAware
    • getStringBuilder

      protected static StringBuilder getStringBuilder()
      Returns a StringBuilder that this Layout implementation can use to write the formatted log event to.
      Returns:
      a StringBuilder
    • trimToMaxSize

      protected static void trimToMaxSize(StringBuilder stringBuilder)
    • getBytes

      protected byte[] getBytes(String s)
    • getCharset

      public Charset getCharset()
      Description copied from interface: StringLayout
      Gets the Charset this layout uses to encode Strings into bytes.
      Specified by:
      getCharset in interface StringLayout
      Returns:
      the Charset this layout uses to encode Strings into bytes.
    • 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>
      Returns:
      The default content type for Strings.
    • getFooter

      public byte[] getFooter()
      Returns the footer, if one is available.
      Specified by:
      getFooter in interface Layout<String>
      Overrides:
      getFooter in class AbstractLayout<String>
      Returns:
      A byte array containing the footer.
    • getFooterSerializer

      public AbstractStringLayout.Serializer getFooterSerializer()
    • getHeader

      public byte[] getHeader()
      Returns the header, if one is available.
      Specified by:
      getHeader in interface Layout<String>
      Overrides:
      getHeader in class AbstractLayout<String>
      Returns:
      A byte array containing the header.
    • getHeaderSerializer

      public AbstractStringLayout.Serializer getHeaderSerializer()
    • getStringBuilderEncoder

      protected Encoder<StringBuilder> getStringBuilderEncoder()
      Returns a Encoder<StringBuilder> that this Layout implementation can use for encoding log events.
      Returns:
      a Encoder<StringBuilder>
    • serializeToBytes

      protected byte[] serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue)
    • serializeToString

      protected String serializeToString(AbstractStringLayout.Serializer serializer)
    • toByteArray

      public byte[] toByteArray(LogEvent event)
      Formats the Log Event as a byte array.
      Specified by:
      toByteArray in interface Layout<String>
      Parameters:
      event - The Log Event.
      Returns:
      The formatted event as a byte array.