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

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

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

JIRA noreply-jira@qos.ch

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

MiNG commented on SLF4J-371:

@Mariano Ruiz, I read the doc of load4j-api, we must choose one of `Supplier<T>` (lazy) or `Object` (eager), but not mixed.

If the arguments contain only one lazy, the others are constants or simple variables.

Using the `Supplier<T>` form will force all arguments being lazy.

LOGGER.debug("format {} {} {}", () -> "const", () -> simpleVariable, () -> Json.serialize(huge))
I'm worrying about too many lambda parameters (invokedynamic) will make the logging high cost.


> 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
slf4j-dev mailing list
[hidden email]