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

Eric Deandrea commented on SLF4J-371:
-------------------------------------

At my last company to get around this we built our own interface that extended 
org.slf4j.Logger with these methods:

 
{code:java}
// code placeholder
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.slf4j.Marker;

public interface Logger extends org.slf4j.Logger {
default void trace(Supplier<String> message) {
if (isTraceEnabled()) {
trace(message.get());
}
}

default void trace(Marker marker, Supplier<String> message) {
if (isTraceEnabled(marker)) {
trace(marker, message.get());
}
}

default void trace(Supplier<String> message, Throwable throwable) {
if (isTraceEnabled()) {
trace(message.get(), throwable);
}
}


default void trace(Marker marker, Supplier<String> message, Throwable 
throwable) {
if (isTraceEnabled(marker)) {
trace(marker, message.get(), throwable);
}
}

default void trace(String format, Supplier<?> arg) {
if (isTraceEnabled()) {
trace(format, arg.get());
}
}

default void trace(String format, Supplier<?> arg1, Supplier<?> arg2) {
if (isTraceEnabled()) {
trace(format, arg1.get(), arg2.get());
}
}

default void trace(String format, Supplier<?>... arguments) {
if (isTraceEnabled()) {
trace(format, Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void trace(Marker marker, String format, Supplier<?> arg) {
if (isTraceEnabled(marker)) {
trace(marker, format, arg.get());
}
}

default void trace(Marker marker, String format, Supplier<?> arg1, Supplier<?> 
arg2) {
if (isTraceEnabled(marker)) {
trace(marker, format, arg1.get(), arg2.get());
}
}

default void trace(Marker marker, String format, Supplier<?>... arguments) {
if (isTraceEnabled(marker)) {
trace(marker, format, 
Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void debug(Supplier<String> message) {
if (isDebugEnabled()) {
debug(message.get());
}
}

default void debug(Marker marker, Supplier<String> message) {
if (isDebugEnabled(marker)) {
debug(marker, message.get());
}
}

default void debug(Supplier<String> message, Throwable throwable) {
if (isDebugEnabled()) {
debug(message.get(), throwable);
}
}

default void debug(Marker marker, Supplier<String> message, Throwable 
throwable) {
if (isDebugEnabled(marker)) {
debug(marker, message.get(), throwable);
}
}

default void debug(String format, Supplier<?> arg) {
if (isDebugEnabled()) {
debug(format, arg.get());
}
}

default void debug(String format, Supplier<?> arg1, Supplier<?> arg2) {
if (isDebugEnabled()) {
debug(format, arg1.get(), arg2.get());
}
}

default void debug(String format, Supplier<?>... arguments) {
if (isDebugEnabled()) {
debug(format, Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void debug(Marker marker, String format, Supplier<?> arg) {
if (isDebugEnabled(marker)) {
debug(marker, format, arg.get());
}
}

default void debug(Marker marker, String format, Supplier<?> arg1, Supplier<?> 
arg2) {
Assert.notNull(arg1, "Argument1 supplier should not be null");
Assert.notNull(arg2, "Argument2 supplier should not be null");

if (isDebugEnabled(marker)) {
debug(marker, format, arg1.get(), arg2.get());
}
}

default void debug(Marker marker, String format, Supplier<?>... arguments) {
if (isDebugEnabled(marker)) {
debug(marker, format, 
Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void info(Supplier<String> message) {
if (isInfoEnabled()) {
info(message.get());
}
}

default void info(Marker marker, Supplier<String> message) {
if (isInfoEnabled(marker)) {
info(marker, message.get());
}
}

default void info(Supplier<String> message, Throwable throwable) {
if (isInfoEnabled()) {
info(message.get(), throwable);
}
}

default void info(Marker marker, Supplier<String> message, Throwable throwable) 
{
if (isInfoEnabled(marker)) {
info(marker, message.get(), throwable);
}
}

default void info(String format, Supplier<?> arg) {
if (isInfoEnabled()) {
info(format, arg.get());
}
}

default void info(String format, Supplier<?> arg1, Supplier<?> arg2) {
if (isInfoEnabled()) {
info(format, arg1.get(), arg2.get());
}
}

default void info(String format, Supplier<?>... arguments) {
if (isInfoEnabled()) {
info(format, Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void info(Marker marker, String format, Supplier<?> arg) {
if (isInfoEnabled(marker)) {
info(marker, format, arg.get());
}
}

default void info(Marker marker, String format, Supplier<?> arg1, Supplier<?> 
arg2) {
if (isInfoEnabled(marker)) {
info(marker, format, arg1.get(), arg2.get());
}
}

default void info(Marker marker, String format, Supplier<?>... arguments) {
if (isInfoEnabled(marker)) {
info(marker, format, 
Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void warn(Supplier<String> message) {
if (isWarnEnabled()) {
warn(message.get());
}
}

default void warn(Marker marker, Supplier<String> message) {
if (isWarnEnabled(marker)) {
warn(marker, message.get());
}
}

default void warn(Supplier<String> message, Throwable throwable) {
if (isWarnEnabled()) {
warn(message.get(), throwable);
}
}

default void warn(Marker marker, Supplier<String> message, Throwable throwable) 
{
if (isWarnEnabled(marker)) {
warn(marker, message.get(), throwable);
}
}

default void warn(String format, Supplier<?> arg) {
if (isWarnEnabled()) {
warn(format, arg.get());
}
}

default void warn(String format, Supplier<?> arg1, Supplier<?> arg2) {
if (isWarnEnabled()) {
warn(format, arg1.get(), arg2.get());
}
}

default void warn(String format, Supplier<?>... arguments) {
if (isWarnEnabled()) {
warn(format, Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void warn(Marker marker, String format, Supplier<?> arg) {
if (isWarnEnabled(marker)) {
warn(marker, format, arg.get());
}
}

default void warn(Marker marker, String format, Supplier<?> arg1, Supplier<?> 
arg2) {
if (isWarnEnabled(marker)) {
warn(marker, format, arg1.get(), arg2.get());
}
}

default void warn(Marker marker, String format, Supplier<?>... arguments) {
if (isWarnEnabled(marker)) {
warn(marker, format, 
Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void error(Supplier<String> message) {
if (isErrorEnabled()) {
error(message.get());
}
}

default void error(Marker marker, Supplier<String> message) {
if (isErrorEnabled(marker)) {
error(marker, message.get());
}
}

default void error(Supplier<String> message, Throwable throwable) {
if (isErrorEnabled()) {
error(message.get(), throwable);
}
}

default void error(Marker marker, Supplier<String> message, Throwable 
throwable) {
if (isErrorEnabled(marker)) {
error(marker, message.get(), throwable);
}
}

default void error(String format, Supplier<?> arg) {
if (isErrorEnabled()) {
error(format, arg.get());
}
}

default void error(String format, Supplier<?> arg1, Supplier<?> arg2) {
if (isErrorEnabled()) {
error(format, arg1.get(), arg2.get());
}
}

default void error(String format, Supplier<?>... arguments) {
if (isErrorEnabled()) {
error(format, Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}

default void error(Marker marker, String format, Supplier<?> arg) {
if (isErrorEnabled(marker)) {
error(marker, format, arg.get());
}
}

default void error(Marker marker, String format, Supplier<?> arg1, Supplier<?> 
arg2) {
if (isErrorEnabled(marker)) {
error(marker, format, arg1.get(), arg2.get());
}
}

default void error(Marker marker, String format, Supplier<?>... arguments) {
if (isErrorEnabled(marker)) {
error(marker, format, 
Stream.of(arguments).map(Supplier::get).toArray(Object[]::new));
}
}
}{code}

> 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)
_______________________________________________
slf4j-dev mailing list
[email protected]
http://mailman.qos.ch/mailman/listinfo/slf4j-dev

Reply via email to