Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2256

GelfLayout does not allow to truncate short message to arbitrary length

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.10.0
    • Fix Version/s: None
    • Component/s: Layouts
    • Labels:
      None
    • Flags:
      Important

      Description

      Hi, 

      using the GelfLayout provided in Log4j2 core, we are unable to truncate the value of the "short_message" field to an arbitrary size for message type other than StringBuilderFormattable.
      Note that StringBuilderFormattable can only be truncated to a value greater than or equal to 1024 bytes. So we cannot rely on this (maybe it should be treated as a separate defect)

      The outcome leads to large amount of data stored in the short message field which impacts performance, storage and indexing.

      Proposed enhancement:
      Short message: Provide ability to use a pattern layout (which will inherit the ability to set length) and/or add the ability to set arbitrary size through the gelf layout attribute
      Full message: Should contain the plain message with the stack trace (the stack trace part was already covered properly)

      Here is the detail of 2.10.0 implementation.

      "org.apache.logging.log4j.core.layout.GelfLayout"
      builder.append("\"short_message\":\"");
              final Message message = event.getMessage();
              if (message instanceof CharSequence) {
                  JsonUtils.quoteAsString(((CharSequence)message), builder);
              } else if (gcFree && message instanceof StringBuilderFormattable) {
                  final StringBuilder messageBuffer = getMessageStringBuilder();
                  try {
                      ((StringBuilderFormattable) message).formatTo(messageBuffer);
                      JsonUtils.quoteAsString(messageBuffer, builder);
                  } finally {
                      trimToMaxSize(messageBuffer);
                  }
              } else {
                  JsonUtils.quoteAsString(toNullSafeString(message.getFormattedMessage()), builder);
              }
      
      "org.apache.logging.log4j.core.layout.AbstractStringLayout"
          protected static final int MAX_STRING_BUILDER_SIZE = Math.max(DEFAULT_STRING_BUILDER_SIZE,
                  size("log4j.layoutStringBuilder.maxSize", 2 * 1024));
          protected static void trimToMaxSize(final StringBuilder stringBuilder) {
              StringBuilders.trimToMaxSize(stringBuilder, MAX_STRING_BUILDER_SIZE);
          }
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              hugo.veillette@iaah.ca Hugo Veillette
            • Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: