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

GC-friendly Message API enhancement

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5
    • Fix Version/s: 2.6
    • Component/s: API
    • Labels:
      None

      Description

      Currently the only way to get the content of a Message object is to call its getFormattedMessage() method, which returns a String. This requires several temporary objects.

      Proposal: introduce a new interface:

      public interface StringBuilderFormattable {
          void formatTo(StringBuilder buffer);
      }
      

      We can either let Message extend StringBuilderFormattable, or only let the current Message implementation classes implement this interface.

      The first option is more elegant: downstream code does not need to use instanceof to detect if the formatTo() method is available. The downside is that user's custom Message implementations need to be modified and recompiled.

      Side note:
      Having a separate interface for this method has the advantage that client code objects that implement this interface can be converted to text without allocating temp objects:

      // gc-free if currentState implements StringBuilderFormattable 
      logger.trace("my state is: {}", currentState);
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                remkop@yahoo.com Remko Popma
                Reporter:
                remkop@yahoo.com Remko Popma
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: