[slf4j-dev] [JIRA] (SLF4J-371) Support the lambda expression in the Logger

QOS.CH (JIRA) noreply-jira at qos.ch
Tue Jan 16 12:03:01 CET 2018


    [ https://jira.qos.ch/browse/SLF4J-371?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=19057#comment-19057 ] 

Joerg commented on SLF4J-371:
-----------------------------

Just came across this issue as Sonar complained about us not using a lambda.

Is the invokedynamic call really a performance issue (couldn't find anything in that direction)? Especially for constant lambda expressions that are possibly inlined anyway? And even if there is a small performance penalty for the constant lambdas, wouldn't the lambda saving the "heavy" evaluation outweigh that?

I propose to put a warning in the JavaDoc and let the user decide if it is neccessary and he is willing to pay the possible performance penalty. The effort of using lambdas everywhere is higher than simply providing objects to the log statements, so the amount of people using lambdas by accident should be non existent.

> Support the lambda expression in the Logger
> -------------------------------------------
>
>                 Key: SLF4J-371
>                 URL: https://jira.qos.ch/browse/SLF4J-371
>             Project: SLF4J
>          Issue Type: Improvement
>          Components: Core API
>    Affects Versions: 1.7.22
>            Reporter: MiNG
>            Assignee: SLF4J developers list
>
> In some cases, we don't want to calculate the expression for logging eagerly cause the performance reason. Then, we would write the code like the following:
> {code:java}
> if (LOGGER.isWarnEnabled())
> {
>  LOGGER.warn("some message: {}", Json.serialize(obj));
> }{code}
> Before JDK8, there is no way to encapsulate the above code, because the expression is always calculated before passed as an argument. So, many "if"s appear in the code and smell badly.
> Now, the lambda expression is supported by JDK8, the above could be simplified like following:
> {code:java}
> LOGGER.warn(formatter -> formatter.format("some message: {}", Json.serialize(obj)));{code}
> With the default method definition in the org.slf4j.Logger:
> {code:java}
> public interface Logger
> {
>  default void warn(Function<MessageFormatter, String> messageSupplier)
>  {
>   if (this.isWarnEnabled())
>   {
>    /* Calculate the expression only if the WARN level logging is enabled. */
>    this.warn(messageSupplier.apply(this.getFormatter()));
>   }
>  }
> }{code}



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)


More information about the slf4j-dev mailing list