[slf4j-dev] Lambda support in SLF4J

Apache ralph.goers at dslextreme.com
Sat Dec 24 17:50:28 CET 2016


The only downside is that anyone who has an implementation of SLF4J will have to add the new methods in order to upgrade.  

Log4j 2 has supported lambdas for quite a while doing exactly what you are proposing here. Obviously the SLF4J Supplier and the Log4j 2 Supplier won’t know anything about each other so the Log4j 2 SLF4J binding will have to wrap the SLF4J Supplier with an instance of a Log4j 2 Supplier. Although this adds slight overhead I don’t see that as a big deal. The only real impact I see is that there will probably be a fair number of new methods added that we will have to implement.  

Of course this will mean that anyone using SLF4J lambdas will be required to upgrade Log4j2 (and Logback for that matter) to at least the earliest version that supports it.

Ralph

> On Dec 22, 2016, at 7:52 AM, Ceki Gülcü <ceki at qos.ch> wrote:
> 
> Hello all,
> 
> Given that JDK 7 earlier are used by over 50% of Java projects, I do not think we can force users to upgrade to Java 8. Thus, SLF4J is unlikely to bump its JDK requirement to version 8 for yet some time.
> 
> On the bright side, there is a way for org.slf4j.Logger to support Java8 lambdas, or just lambdas, without requiring Java 8. As stated in [1], the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a @FunctionalInterface annotation is present on the interface declaration.
> 
> This means that we can create a Supplier interface such as
> 
> public interface Supplier<T> {
>    T get();
> }
> 
> and then add the following method to org.slf4j.Logger interface.
> 
> package org.slf4j;
> 
> public interface Logger {
>  ....
>  public void debug(Supplier<T> supplier);
>  ....
> }
> 
> This will add lambda support without requiring Java 8 support at build time.
> 
> The downside of this approach is that implementations of the org.slf4j.Logger interface as found in logback and modules such as slf4j-simple, slf4j-log412, slf4j-jdk14 will need to be updated. Given that the number of such implementations is limited, I think the requirement is quite acceptable.
> 
> Your comments are welcome,
> 
> --
> Ceki Gülcü
> 
> 
> [1] https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
> _______________________________________________
> slf4j-dev mailing list
> slf4j-dev at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-dev




More information about the slf4j-dev mailing list