[slf4j-dev] [JIRA] Updates for SLF4J-371: Support the lambda expression in the Logger

QOS.CH (JIRA) noreply-jira at qos.ch
Sun Dec 26 23:09:00 CET 2021


SLF4J / SLF4J-371 [Resolved]
Support the lambda expression in the Logger

==============================

Here's what changed in this issue in the last few minutes.

There is 1 comment.

View or comment on issue using this link
https://jira.qos.ch/browse/SLF4J-371

==============================
 1 comment
------------------------------

Alexander Kiselyov on 26/Dec/21 10:57 PM
[~ekolotyluk], I might be missing your point, but I guess any of these 2 samples does what you intended to:

 
{code:java}
Supplier<String> task = () -> {
    System.out.println("task evaluated");
    return "called";
};
logger.atDebug().addArgument(task).log("Lazy logging {}");{code}
 
{code:java}
logger.atDebug().addArgument(() -> {
    System.out.println("task evaluated");
    return "called";
}).log("Lazy logging {}");
{code}
 

This has an extra call compared to your desired sample (in general N calls will be required, where N is a number of parameters, since there's no varargs version of [addArgument|https://www.slf4j.org/api/org/slf4j/spi/LoggingEventBuilder.html#addArgument(java.util.function.Supplier)] or {{addKeyValue}}, which may actually be useful to have). The version you published ({{log("Lazy logging {}", task.call())}}) simply can't work in any other way: {{task.call()}} is not a [method reference expression|https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.13] ("refer to the invocation of a method without actually performing the invocation"), but just a regular method invocation, so will obviously be executed by JVM in order to evaluate all the {{log()}} method arguments prior to its actual invocation (again, according to Java Language Specification).

 


==============================
 This message was sent by Atlassian Jira (v8.8.0#808000-sha1:e2c7e59)



More information about the slf4j-dev mailing list