Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLF4J-256: Allow use of lambda to construct build event #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 59 additions & 24 deletions slf4j-api/src/main/java/org/slf4j/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@

import org.slf4j.event.Level;
import org.slf4j.spi.DefaultLoggingEventBuilder;
import org.slf4j.spi.FluentLogApiStub;
import org.slf4j.spi.LoggingEventBuilder;
import org.slf4j.spi.NOPLoggingEventBuilder;
import org.slf4j.spi.NopFluentApiStub;

import java.util.function.Consumer;

/**
* The org.slf4j.Logger interface is the main user entry point of SLF4J API.
Expand Down Expand Up @@ -96,30 +99,43 @@ public interface Logger {
public String getName();

/**
* Make a new {@link LoggingEventBuilder} instance as appropriate for this logger and the
* Make a new {@link FluentLogApiStub} instance as appropriate for this logger and the
* desired {@link Level} passed as parameter. If this Logger is disabled for the given Level, then
* a {@link NOPLoggingEventBuilder} is returned.
* a {@link NopFluentApiStub} is returned.
*
*
* @param level desired level for the event builder
* @return a new {@link LoggingEventBuilder} instance as appropriate for this logger
* @since 2.0
*/
default public LoggingEventBuilder makeLoggingEventBuilder(Level level) {
default public FluentLogApiStub makeLoggingEventBuilder(Level level) {
if (isEnabledForLevel(level)) {
return new DefaultLoggingEventBuilder(this, level);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}

default public FluentLogApiStub atLevel(Level level) {
return makeLoggingEventBuilder(level);
}


/**
* A convenient alias for {@link #makeLoggingEventBuilder}.
*
* Use provided LoggingEventBuilder consumer to construct the logging event, and log at provided level.
*
* <code>
* logger.atDebug(log->log.message("Temperature rise from {} to {}")
* </code>
*
* @since 2.0
*/
default public LoggingEventBuilder atLevel(Level level) {
return makeLoggingEventBuilder(level);
default public void atLevel(Level level, Consumer<LoggingEventBuilder> eventBuilderConsumer) {
if (isEnabledForLevel(level)) {
DefaultLoggingEventBuilder eventBuilder= new DefaultLoggingEventBuilder(this, level);
eventBuilderConsumer.accept(eventBuilder);
eventBuilder.log();
}
}


Expand Down Expand Up @@ -233,16 +249,19 @@ default public boolean isEnabledForLevel(Level level) {
/**
* Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level.
*
* @return LoggingEventBuilder instance as appropriate for level TRACE
* @return FluentLogApiStub instance as appropriate for level TRACE
* @since 2.0
*/
default public LoggingEventBuilder atTrace() {
default public FluentLogApiStub atTrace() {
if (isTraceEnabled()) {
return makeLoggingEventBuilder(TRACE);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}
default public void atTrace(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
this.atLevel(TRACE, eventBuilderConsumer);
}

/**
* Log a message with the specific Marker at the TRACE level.
Expand Down Expand Up @@ -429,17 +448,21 @@ default public LoggingEventBuilder atTrace() {
/**
* Entry point for fluent-logging for {@link org.slf4j.event.Level#DEBUG} level.
*
* @return LoggingEventBuilder instance as appropriate for level DEBUG
* @return FluentLogApiStub instance as appropriate for level DEBUG
* @since 2.0
*/
default public LoggingEventBuilder atDebug() {
default public FluentLogApiStub atDebug() {
if (isDebugEnabled()) {
return makeLoggingEventBuilder(DEBUG);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}

default public void atDebug(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
this.atLevel(DEBUG, eventBuilderConsumer);
}

/**
* Is the logger instance enabled for the INFO level?
*
Expand Down Expand Up @@ -568,16 +591,19 @@ default public LoggingEventBuilder atDebug() {
/**
* Entry point for fluent-logging for {@link org.slf4j.event.Level#INFO} level.
*
* @return LoggingEventBuilder instance as appropriate for level INFO
* @return FluentLogApiStub instance as appropriate for level INFO
* @since 2.0
*/
default public LoggingEventBuilder atInfo() {
default public FluentLogApiStub atInfo() {
if (isInfoEnabled()) {
return makeLoggingEventBuilder(INFO);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}
default public void atInfo(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
this.atLevel(INFO, eventBuilderConsumer);
}

/**
* Is the logger instance enabled for the WARN level?
Expand Down Expand Up @@ -708,16 +734,19 @@ default public LoggingEventBuilder atInfo() {
/**
* Entry point for fluent-logging for {@link org.slf4j.event.Level#WARN} level.
*
* @return LoggingEventBuilder instance as appropriate for level WARN
* @return FluentLogApiStub instance as appropriate for level WARN
* @since 2.0
*/
default public LoggingEventBuilder atWarn() {
default public FluentLogApiStub atWarn() {
if (isWarnEnabled()) {
return makeLoggingEventBuilder(WARN);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}
default public void atWarn(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
this.atLevel(WARN, eventBuilderConsumer);
}

/**
* Is the logger instance enabled for the ERROR level?
Expand Down Expand Up @@ -849,15 +878,21 @@ default public LoggingEventBuilder atWarn() {
/**
* Entry point for fluent-logging for {@link org.slf4j.event.Level#ERROR} level.
*
* @return LoggingEventBuilder instance as appropriate for level ERROR
* @return FluentLogApiStub instance as appropriate for level ERROR
* @since 2.0
*/
default public LoggingEventBuilder atError() {
default public FluentLogApiStub atError() {
if (isErrorEnabled()) {
return makeLoggingEventBuilder(ERROR);
} else {
return NOPLoggingEventBuilder.singleton();
return NopFluentApiStub.singleton();
}
}

default public void atError(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
this.atLevel(ERROR, eventBuilderConsumer);
}



}
57 changes: 44 additions & 13 deletions slf4j-api/src/main/java/org/slf4j/helpers/SubstituteLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Queue;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.event.EventRecodingLogger;
import org.slf4j.event.Level;
import org.slf4j.event.LoggingEvent;
import org.slf4j.event.SubstituteLoggingEvent;
import org.slf4j.spi.FluentLogApiStub;
import org.slf4j.spi.LoggingEventBuilder;

/**
Expand Down Expand Up @@ -69,15 +71,21 @@ public String getName() {
}

@Override
public LoggingEventBuilder makeLoggingEventBuilder(Level level) {
public FluentLogApiStub makeLoggingEventBuilder(Level level) {
return delegate().makeLoggingEventBuilder(level);
}

@Override
public LoggingEventBuilder atLevel(Level level) {
public FluentLogApiStub atLevel(Level level) {
return delegate().atLevel(level);
}


@Override
public void atLevel(Level level, Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atLevel(level, eventBuilderConsumer);
}


@Override
public boolean isEnabledForLevel(Level level) {
return delegate().isEnabledForLevel(level);
Expand Down Expand Up @@ -142,10 +150,15 @@ public void trace(Marker marker, String msg, Throwable t) {
}

@Override
public LoggingEventBuilder atTrace() {
public FluentLogApiStub atTrace() {
return delegate().atTrace();
}


@Override
public void atTrace(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atTrace(eventBuilderConsumer);
}

@Override
public boolean isDebugEnabled() {
return delegate().isDebugEnabled();
Expand Down Expand Up @@ -207,10 +220,15 @@ public void debug(Marker marker, String msg, Throwable t) {
}

@Override
public LoggingEventBuilder atDebug() {
public FluentLogApiStub atDebug() {
return delegate().atDebug();
}


@Override
public void atDebug(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atDebug(eventBuilderConsumer);
}

@Override
public boolean isInfoEnabled() {
return delegate().isInfoEnabled();
Expand Down Expand Up @@ -273,11 +291,15 @@ public void info(Marker marker, String msg, Throwable t) {
}

@Override
public LoggingEventBuilder atInfo() {
public FluentLogApiStub atInfo() {
return delegate().atInfo();
}


@Override
public void atInfo(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atInfo(eventBuilderConsumer);
}

@Override
public boolean isWarnEnabled() {
return delegate().isWarnEnabled();
Expand Down Expand Up @@ -338,11 +360,15 @@ public void warn(Marker marker, String msg, Throwable t) {
}

@Override
public LoggingEventBuilder atWarn() {
public FluentLogApiStub atWarn() {
return delegate().atWarn();
}


@Override
public void atWarn(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atWarn(eventBuilderConsumer);
}


@Override
public boolean isErrorEnabled() {
Expand Down Expand Up @@ -405,10 +431,15 @@ public void error(Marker marker, String msg, Throwable t) {
}

@Override
public LoggingEventBuilder atError() {
public FluentLogApiStub atError() {
return delegate().atError();
}


@Override
public void atError(Consumer<LoggingEventBuilder> eventBuilderConsumer) {
delegate().atError(eventBuilderConsumer);
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Loading