[
https://issues.apache.org/jira/browse/LOG4J2-3113?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17378067#comment-17378067
]
Markus Spann commented on LOG4J2-3113:
--------------------------------------
{{System.Logger}} is public API that any application may use.
The JDK developers state their API should first of all cover the needs of the
JDK.
It so happens that these needs are pretty much those of any application.
System.Logger is an alternative to {{slf4j}} and {{log4j-api}}.
The three APIs by and large have identical feature sets.
I always favor the JDK over external libraries if feature set and quality are
similar.
My emphasis on (and example of) System.Logger and log4j2 may have been
confusing in my posts.
The combination System.Logger + log4j2 is what I like to use.
All APIs offer a varargs signature of type Object:
System.Logger: {{java.lang.System.Logger.log(Level, String, Object...)}}
slf4j: {{org.slf4j.Logger.<level>(String, Object...)}}
log4j2-api: {{org.apache.logging.log4j.Logger.<level>(String, Object...)}}
My suggestion is that log4j2, as the concrete implementation, should deal with
objects of type Supplier found in this object array on the above signatures in
a better, more creative way by calling get() and not toString() on Suppliers
(and toString() on other types).
> Support Supplier<> in varargs log message parameters
> ----------------------------------------------------
>
> Key: LOG4J2-3113
> URL: https://issues.apache.org/jira/browse/LOG4J2-3113
> Project: Log4j 2
> Issue Type: Improvement
> Components: API
> Affects Versions: 2.14.1
> Reporter: Markus Spann
> Priority: Minor
>
> 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.
> {code:java}
> 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));{code}
> {code:java}
> // compiles but calls toString() rather than Supplier.get()
> logger.log(Level.DEBUG, "Contents of array: {}", (Supplier<String>) () ->
> Arrays.deepToString(array));{code}
> 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.
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)