[
https://issues.apache.org/jira/browse/LOG4J2-3113?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17371906#comment-17371906
]
Markus Spann commented on LOG4J2-3113:
--------------------------------------
{quote}I have always been under the impression that Java doesn't really care
what the actual interface is, just that there is one declared that matches the
lambda expression.
{quote}
I would have worded it differently (the lambda expression must match the method
signature).
[~ggregory], I don't think we're on the same page yet:
My case uses Java's {{System.getLogger}}.
Log4j2 is the run-time logging implementation e.g. a run-time not compile-time
dependency.
Here is the Java System Logger call with two {{Supplier<String>}} on the
varargs signature:
{code:java}
java.util.function.Supplier<String> strSupplier1 = () -> "supplied1";
java.util.function.Supplier<String> strSupplier2 = () -> "supplied2";
logger.log(Level.INFO, "Log with suppliers using varargs signature: {}, {}",
strSupplier1, strSupplier2);{code}
How the call enters log4j2:
{code:java}
Logger(AbstractLogger).logMessage(String, Level, Marker, String, Object...)
line: 2027
Logger(AbstractLogger).logIfEnabled(String, Level, Marker, String, Object...)
line: 1891
Log4jSystemLogger.log(System$Logger$Level, ResourceBundle, String, Object...)
line: 100
Log4jSystemLogger.log(System$Logger$Level, String, Object...) line: 90
TestLog4j2Config.main(String[]) line: 33
{code}
What gets logged:
{noformat}
INFO [main] TestLog4j2Config - TestLog4j2Config.main() - Log with suppliers
using varargs signature: TestLog4j2Config$StringSupplier@f99f5e0,
TestLog4j2Config$StringSupplier@6aa61224{noformat}
What should get logged:
{noformat}
INFO [main] TestLog4j2Config - TestLog4j2Config.main() - Log with suppliers
using varargs signature: supplied1, supplied2{noformat}
what do you think? shall I send you a PR?
> 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)