From 154b4e4afa6b21721f58c8a97fa70e58adb7bde3 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Fri, 22 Dec 2023 10:52:24 +0100 Subject: [PATCH] issues/375 add arg(primitiveType) methods in LoggingEventBuilder interface Signed-off-by: Ceki Gulcu --- slf4j-api/src/main/java/org/slf4j/Logger.java | 31 +++- .../slf4j/spi/DefaultLoggingEventBuilder.java | 7 +- .../org/slf4j/spi/LoggingEventBuilder.java | 141 +++++++++++++++++- .../org/slf4j/spi/NOPLoggingEventBuilder.java | 100 ++++++++++++- 4 files changed, 268 insertions(+), 11 deletions(-) diff --git a/slf4j-api/src/main/java/org/slf4j/Logger.java b/slf4j-api/src/main/java/org/slf4j/Logger.java index 2803f7829..b3d63c96b 100644 --- a/slf4j-api/src/main/java/org/slf4j/Logger.java +++ b/slf4j-api/src/main/java/org/slf4j/Logger.java @@ -116,9 +116,9 @@ default public LoggingEventBuilder makeLoggingEventBuilder(Level level) { } /** - * Make a new {@link LoggingEventBuilder} instance as appropriate for this logger and the + *

Make a new {@link LoggingEventBuilder} 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 NOPLoggingEventBuilder} is returned. This is the main optimization in the fluent API.

* * * @param level desired level for the event builder @@ -243,8 +243,11 @@ default public boolean isEnabledForLevel(Level level) { public boolean isTraceEnabled(Marker marker); /** - * Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level. - * + * Entry point for fluent-logging for {@link org.slf4j.event.Level#TRACE} level. + * + *

If this logger is disabled for the TRACE level, then a {@link NOPLoggingEventBuilder} instance is returned. + * As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

+ * * @return LoggingEventBuilder instance as appropriate for level TRACE * @since 2.0 */ @@ -441,7 +444,10 @@ default public LoggingEventBuilder atTrace() { /** * Entry point for fluent-logging for {@link org.slf4j.event.Level#DEBUG} level. - * + * + *

If this logger is disabled for the DEBUG level, then a {@link NOPLoggingEventBuilder} instance is returned. + * As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

+ * * @return LoggingEventBuilder instance as appropriate for level DEBUG * @since 2.0 */ @@ -582,7 +588,10 @@ default public LoggingEventBuilder atDebug() { /** * Entry point for fluent-logging for {@link org.slf4j.event.Level#INFO} level. - * + * + *

If this logger is disabled for the INFO level, then a {@link NOPLoggingEventBuilder} instance is returned. + * As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

+ * @return LoggingEventBuilder instance as appropriate for level INFO * @since 2.0 */ @@ -723,7 +732,10 @@ default public LoggingEventBuilder atInfo() { /** * Entry point for fluent-logging for {@link org.slf4j.event.Level#WARN} level. - * + * + *

If this logger is disabled for the WARN level, then a {@link NOPLoggingEventBuilder} instance is returned. + * As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

+ * * @return LoggingEventBuilder instance as appropriate for level WARN * @since 2.0 */ @@ -865,7 +877,10 @@ default public LoggingEventBuilder atWarn() { /** * Entry point for fluent-logging for {@link org.slf4j.event.Level#ERROR} level. - * + * + *

If this logger is disabled for the ERROR level, then a {@link NOPLoggingEventBuilder} instance is returned. + * As the name indicates, this builder does not perform any operations.

+ * * @return LoggingEventBuilder instance as appropriate for level ERROR * @since 2.0 */ diff --git a/slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java b/slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java index 66652b070..d2cd1cccd 100755 --- a/slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java +++ b/slf4j-api/src/main/java/org/slf4j/spi/DefaultLoggingEventBuilder.java @@ -34,7 +34,11 @@ import org.slf4j.event.LoggingEvent; /** - * Default implementation of {@link LoggingEventBuilder} + * Default implementation of {@link LoggingEventBuilder}. + * + *

It is assumed that when

+ * + * @since 2.0.0 */ public class DefaultLoggingEventBuilder implements LoggingEventBuilder, CallerBoundaryAware { @@ -76,6 +80,7 @@ public LoggingEventBuilder addArgument(Object p) { return this; } + @Override public LoggingEventBuilder addArgument(Supplier objectSupplier) { loggingEvent.addArgument(objectSupplier.get()); diff --git a/slf4j-api/src/main/java/org/slf4j/spi/LoggingEventBuilder.java b/slf4j-api/src/main/java/org/slf4j/spi/LoggingEventBuilder.java index 55e24cfb5..9443c834c 100755 --- a/slf4j-api/src/main/java/org/slf4j/spi/LoggingEventBuilder.java +++ b/slf4j-api/src/main/java/org/slf4j/spi/LoggingEventBuilder.java @@ -58,6 +58,7 @@ public interface LoggingEventBuilder { /** * Add an argument to the event being built. + * Synonymous with {@link #arg(Object)}. * * @param p an Object to add. * @return a LoggingEventBuilder, usually this. @@ -66,8 +67,21 @@ public interface LoggingEventBuilder { LoggingEventBuilder addArgument(Object p); /** - * Add an argument supplier to the event being built. + * Add an argument to the event being built. + * Synonymous with {@link #addArgument(Object)}. * + * @param p an Object to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + @CheckReturnValue + default LoggingEventBuilder arg(Object p) { + return addArgument(p); + } + + /** + *

Add an argument supplier to the event being built. Synonymous with {@link #arg(Supplier)}. + *

* @param objectSupplier an Object supplier to add. * @return a LoggingEventBuilder, usually this. */ @@ -75,6 +89,131 @@ public interface LoggingEventBuilder { LoggingEventBuilder addArgument(Supplier objectSupplier); + /** + *

Add an argument supplier to the event being built. Synonymous with {@link #addArgument(Supplier)}. + *

+ * + * @param objectSupplier an Object supplier to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + @CheckReturnValue + default LoggingEventBuilder arg(Supplier objectSupplier) { + return addArgument(objectSupplier); + } + + /** + * Add a value of type boolean to the event being built. + * + *

The default implementation simply casts to Boolean. However, However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param b a value of type boolean value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(boolean b) { + return addArgument((Boolean) b); + } + + /** + * Add a value of type char to the event being built. + * + *

The default implementation simply casts to Character. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param c a value of type char value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(char c) { + return addArgument((Character) c); + } + + /** + * Add a value of type byte to the event being built. + * + *

The default implementation simply casts to Byte. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param b a value of type byte value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(byte b) { + return addArgument((Byte) b); + } + + /** + * Add a value of type short to the event being built. + * + *

The default implementation simply casts to Short. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param s a value of type short value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(short s) { + return addArgument((Short) s); + } + + /** + * Add a value of type int to the event being built. + * + *

The default implementation simply casts to Integer. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param i a value of type int value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(int i) { + return addArgument((Integer) i); + } + + /** + * Add a value of type long to the event being built. + * + *

The default implementation simply casts to Long. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param l a value of type long value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(long l) { + return addArgument((Long) l); + } + + /** + * Add a value of type float to the event being built. + * + *

The default implementation simply casts to Float. However, the NOP implementation, i.e. + * {@link NOPLoggingEventBuilder}, skips the cast.

+ * + * @param f a value of type float value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default public LoggingEventBuilder arg(float f) { + return addArgument((Float) f); + } + + /** + * Add a value of type double to the event being built. + * + *

The default implementation simply casts to Double. However, the NOP implementation skips the cast.

+ * + * @param d a value of type double value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + default LoggingEventBuilder arg(double d) { + return arg((Double) d); + } + + /** * Add a {@link org.slf4j.event.KeyValuePair key value pair} to the event being built. * diff --git a/slf4j-api/src/main/java/org/slf4j/spi/NOPLoggingEventBuilder.java b/slf4j-api/src/main/java/org/slf4j/spi/NOPLoggingEventBuilder.java index e356b47fc..2561f4772 100755 --- a/slf4j-api/src/main/java/org/slf4j/spi/NOPLoggingEventBuilder.java +++ b/slf4j-api/src/main/java/org/slf4j/spi/NOPLoggingEventBuilder.java @@ -2,16 +2,22 @@ import java.util.function.Supplier; +import org.slf4j.Logger; import org.slf4j.Marker; import org.slf4j.event.Level; /** *

A no-operation implementation of {@link LoggingEventBuilder}.

* - *

As the name indicates, the method in this class do nothing, except when a return value is expected + *

As the name indicates, the methods in this class do nothing, except when a return value is expected * in which case a singleton, i.e. the unique instance of this class is returned. *

The default implementations of {@link Logger#atTrace()}, {@link Logger#atDebug()} , {@link Logger#atInfo()}, + * {@link Logger#atWarn()} and {@link Logger#atError()}, return an instance of {@link NOPLoggingEventBuilder} + * when the relevant level is disabled for current logger. This is the core optimization in the fluent API.

+ * + * * @author Ceki Gülcü * @since 2.0.0 * @@ -48,6 +54,98 @@ public LoggingEventBuilder addArgument(Supplier objectSupplier) { return singleton(); } + /** + * Add a value of type boolean to the event being built. + * + *

The default implementation simply casts to Boolean. However, the NOP implementation skips the cast.

+ * + * @param b a value of type boolean value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(boolean b) { + return singleton(); + } + + /** + * Add a value of type char to the event being built. + * + *

The default implementation simply casts to Character. However, the NOP implementation skips the cast.

+ * + * @param c a value of type char value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(char c) { + return singleton(); + } + + /** + * Add a value of type byte to the event being built. + * + *

The default implementation simply casts to Byte. However, the NOP implementation skips the cast.

+ * + * @param b a value of type byte value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(byte b) { + return singleton(); + } + + /** + * Add a value of type short to the event being built. + * + *

The default implementation simply casts to Short. However, the NOP implementation skips the cast.

+ * + * @param s a value of type short value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(short s) { + return singleton(); + } + + /** + * Add a value of type int to the event being built. + * + *

The default implementation simply casts to Integer. However, the NOP implementation skips the cast.

+ * + * @param i a value of type int value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(int i) { + return singleton(); + } + + /** + * Add a value of type long to the event being built. + * + *

The default implementation simply casts to Long. However, the NOP implementation skips the cast.

+ * + * @param l a value of type long value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(long l) { + return singleton(); + } + + /** + * Add a value of type float to the event being built. + * + *

The default implementation simply casts to Float. However, the NOP implementation skips the cast.

+ * + * @param f a value of type float value to add. + * @return a LoggingEventBuilder, usually this. + * @since 2.1.0 + */ + public LoggingEventBuilder arg(float f) { + return singleton(); + } + + @Override public LoggingEventBuilder addKeyValue(String key, Object value) { return singleton();