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

Support Supplier<> in varargs log message parameters

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 2.14.1
    • None
    • API
    • None

    Description

      It would be very useful, to give special treatment to message parameters of type java.util.function.Supplier<T> by calling get() rather than toString() on it while creating the log message.

      Message parameters (i.e. method calls that return the parameter) that are expensive to compute can thus be computed lazily.

      What's more, the logging call reads more naturally just like all other logging calls that use wildcards and parameters.

      java.lang.System.Logger logger = System.getLogger("toto");
      
      // already supported style using a single Supplier
      logger.log(Level.DEBUG, () -> "Contents of array: " + Arrays.deepToString(array));
      // compiles but calls toString() rather than Supplier.get()
       logger.log(Level.DEBUG, "Contents of array: {}", (Supplier<String>) () -> Arrays.deepToString(array));

      I've looked through the log4j2 Jira and found issue LOG4J2-599 in which a similar request was previously discussed without actually adding the feature to log4j2.

      (Note: Strangely Java requires the cast to (Supplier<String>).)

      My suggestion is to add Supplier support by enhancing method org.apache.logging.log4j.util.StringBuilders.appendSpecificTypes().

      I will provide a Pull Request if you agree to the enhancement.

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            spannjp Markus Spann
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: