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

QOS.CH (JIRA) noreply-jira at qos.ch
Fri Jun 21 17:09:01 CEST 2019


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

Dean Hiller commented on SLF4J-371:
-----------------------------------

@Federico ... I think perhaps I am not being clear.  Sure slf4j-fluent just like our wrapper works fine 'by itself'.  If every 3rd party library also uses slf4j-fluent(or any wrapper), there would also be know issue

However, say you have a 3rd party library that you bring in that uses slf4j, and say your code uses the wrapper.  The log statement by the 3rd party is 1 stack frame away while the wrapper is 2 stack frames away.  This means, log statements that use your wrapper can work fine while the 3rd party log statements no longer work as it goes up the stack 2 times and finds the wrong frame/file/line location.  

I just went and tested this and sure enough...it does not work :(.(but I mean, it should be obvious since slf4j uses the stack frames to get this info)   In webpieces development, it is my preference that I can click 'any' log statement and it will take me to my code or 3rd party code correctly.  unfortunately, we have to back out of all wrappers including slf4j-fluent.

later,

Dean

> 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
>             Fix For: 2.0
>
>
> 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