diff --git a/README.md b/README.md index c215bda..4b86aa1 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ application would use runtime-scope for this provider as a dependency: ``` Note: Only one logging provider such as this should be in effect at run-time. If multiple providers end up in the final -build of an application, somehow, then the `elf4j.logger.factory.fqcn` system property will have to be used to select +build of an application, somehow, then the `elf4j.service.provider.fqcn` system property will have to be used to select the desired provider. ``` diff --git a/pom.xml b/pom.xml index 5ca2f7b..fd1803a 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ io.github.elf4j elf4j-slf4j - 4.0.0 + 4.1.0 jar elf4j-slf4j @@ -68,7 +68,7 @@ io.github.elf4j elf4j - 4.0.0 + 4.1.0 org.slf4j @@ -136,6 +136,31 @@ + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + + spotless-apply-id + process-sources + + apply + + + + + + + 2.43.0 + + true + + + + + + diff --git a/src/main/java/elf4j/slf4j/CallerBoundaryImmutableLoggingEventBuilder.java b/src/main/java/elf4j/slf4j/CallerBoundaryImmutableLoggingEventBuilder.java index 21d0b20..f7edb86 100644 --- a/src/main/java/elf4j/slf4j/CallerBoundaryImmutableLoggingEventBuilder.java +++ b/src/main/java/elf4j/slf4j/CallerBoundaryImmutableLoggingEventBuilder.java @@ -37,6 +37,6 @@ public CallerBoundaryImmutableLoggingEventBuilder(Logger logger, Level level, St @Override public void setCallerBoundary(String fqcn) { - //noop + // noop } } diff --git a/src/main/java/elf4j/slf4j/Slf4jLogger.java b/src/main/java/elf4j/slf4j/Slf4jLogger.java index b969d7f..0245350 100644 --- a/src/main/java/elf4j/slf4j/Slf4jLogger.java +++ b/src/main/java/elf4j/slf4j/Slf4jLogger.java @@ -25,21 +25,20 @@ package elf4j.slf4j; +import static elf4j.Level.*; + import elf4j.Level; import elf4j.Logger; import elf4j.util.NoopLogger; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; +import javax.annotation.concurrent.Immutable; import lombok.NonNull; import lombok.ToString; import org.slf4j.LoggerFactory; import org.slf4j.spi.LoggingEventBuilder; -import javax.annotation.concurrent.Immutable; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Supplier; - -import static elf4j.Level.*; - @Immutable @ToString class Slf4jLogger implements Logger { @@ -48,8 +47,11 @@ class Slf4jLogger implements Logger { private static final String CALLER_BOUNDARY_FQCN = Slf4jLogger.class.getName(); private static final EnumMap LEVEL_MAP = setLevelMap(); private static final EnumMap> LOGGER_CACHE = initLoggerCache(); + @NonNull private final String name; + @NonNull private final Level level; + @NonNull private final org.slf4j.Logger delegateLogger; private Slf4jLogger(@NonNull String name, @NonNull Level level) { @@ -71,7 +73,7 @@ private static Slf4jLogger getLogger(String name) { return getLogger(name, DEFAULT_LEVEL); } - private static EnumMap> initLoggerCache() { + private static @NonNull EnumMap> initLoggerCache() { EnumMap> loggerCache = new EnumMap<>(Level.class); EnumSet.allOf(Level.class).forEach(level -> loggerCache.put(level, new ConcurrentHashMap<>())); return loggerCache; @@ -95,7 +97,7 @@ private static StackTraceElement serviceClient() { + Arrays.toString(stackTrace)); } - private static EnumMap setLevelMap() { + private static @NonNull EnumMap setLevelMap() { EnumMap levelMap = new EnumMap<>(Level.class); levelMap.put(TRACE, org.slf4j.event.Level.TRACE); levelMap.put(DEBUG, org.slf4j.event.Level.DEBUG); @@ -177,9 +179,10 @@ private void slf4jLog(Throwable t, Object message, Object... args) { if (!isEnabled()) { return; } - LoggingEventBuilder loggingEventBuilder = new CallerBoundaryImmutableLoggingEventBuilder(delegateLogger, - translate(this.level), - CALLER_BOUNDARY_FQCN).setMessage(Objects.toString(supply(message))).setCause(t); + LoggingEventBuilder loggingEventBuilder = new CallerBoundaryImmutableLoggingEventBuilder( + delegateLogger, translate(this.level), CALLER_BOUNDARY_FQCN) + .setMessage(Objects.toString(supply(message))) + .setCause(t); if (args != null) { for (Object arg : args) { loggingEventBuilder = loggingEventBuilder.addArgument(supply(arg)); diff --git a/src/main/java/elf4j/slf4j/Slf4jLoggerFactory.java b/src/main/java/elf4j/slf4j/Slf4jLoggerFactory.java index c16d7fa..a11a11b 100644 --- a/src/main/java/elf4j/slf4j/Slf4jLoggerFactory.java +++ b/src/main/java/elf4j/slf4j/Slf4jLoggerFactory.java @@ -28,9 +28,7 @@ import elf4j.Logger; import elf4j.spi.LogServiceProvider; -/** - * - */ +/** */ public class Slf4jLoggerFactory implements LogServiceProvider { @Override public Logger logger() { diff --git a/src/test/java/elf4j/slf4j/Slf4jLoggerTest.java b/src/test/java/elf4j/slf4j/Slf4jLoggerTest.java index 3f8ceca..fdb249a 100644 --- a/src/test/java/elf4j/slf4j/Slf4jLoggerTest.java +++ b/src/test/java/elf4j/slf4j/Slf4jLoggerTest.java @@ -25,16 +25,15 @@ package elf4j.slf4j; +import static org.junit.jupiter.api.Assertions.*; + import elf4j.Level; import elf4j.Logger; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; - import java.util.Arrays; import java.util.function.Supplier; import java.util.stream.Collectors; - -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; class Slf4jLoggerTest { public static final Logger LOGGER = Logger.instance(); @@ -64,7 +63,7 @@ void object() { @Test void supplier() { - LOGGER.atTrace().log((Supplier) () -> "supplier message"); + LOGGER.atTrace().log(() -> "supplier message"); } @Test @@ -75,10 +74,11 @@ void messageAndArgs() { @Test void messageAndSuppliers() { LOGGER.atWarn() - .log("message supplier arg1 {}, arg2 {}, arg3 {}", - (Supplier) () -> "a11111", - "a22222", - (Supplier) () -> Arrays.stream(new Object[] { "a33333" }).collect(Collectors.toList())); + .log( + "message supplier arg1 {}, arg2 {}, arg3 {}", + () -> "a11111", + () -> "a22222", + () -> Arrays.stream(new Object[] {"a33333"}).collect(Collectors.toList())); } @Test @@ -93,7 +93,7 @@ void throwableAndMessage() { @Test void throwableAndSupplier() { - LOGGER.atError().log(new Exception("ex message"), (Supplier) () -> "supplier log message"); + LOGGER.atError().log(new Exception("ex message"), () -> "supplier log message"); } @Test @@ -104,11 +104,13 @@ void throwableAndMessageAndArgs() { @Test void throwableAndMessageAndSupplierArgs() { LOGGER.atError() - .log(new Exception("ex message"), + .log( + new Exception("ex message"), "log message with supplier arg1 {}, arg2 {}, arg3 {}", "a11111", "a22222", - (Supplier) () -> Arrays.stream(new Object[] { "a33333" }).collect(Collectors.toList())); + (Supplier) + () -> Arrays.stream(new Object[] {"a33333"}).collect(Collectors.toList())); } } @@ -134,14 +136,17 @@ void messagesArgsAndGuards() { assertEquals(logger.getName(), ((Slf4jLogger) debug).getName()); assertEquals(Level.DEBUG, debug.getLevel()); if (debug.isEnabled()) { - debug.log("a {} guarded by a {}, so {} is created {} DEBUG level is {}", + debug.log( + "a {} guarded by a {}, so {} is created {} DEBUG level is {}", "long message", "level check", "no message object", "unless", "enabled by the configuration of the logging provider"); } - debug.log((Supplier) () -> "alternative to the level guard, using a supplier function should achieve the same goal, pending quality of the logging provider"); + debug.log( + () -> + "alternative to the level guard, using a supplier function should achieve the same goal, pending quality of the logging provider"); } @Test @@ -153,25 +158,28 @@ void throwableAndMessageAndArgs() { error.log(ex); error.log(ex, "level set omitted but we know the level is Level.ERROR"); error.atWarn() - .log(ex, + .log( + ex, "the log level switched to WARN on the fly. that is, {} returns a {} and {} Logger {}", "atWarn()", "different", "immutable", "instance"); error.atError() - .log(ex, + .log( + ex, "here the {} call is {} because the {} instance is {}, and the instance's log level has and will always be Level.ERROR", "atError()", "unnecessary", "error logger", "immutable"); - error.log(ex, + error.log( + ex, "now at Level.ERROR, together with the exception stack trace, logging some items expensive to compute: item1 {}, item2 {}, item3 {}, item4 {}, ...", "i11111", "i22222", "i33333", - (Supplier) () -> Arrays.stream(new Object[] { "i44444" }).collect(Collectors.toList())); + (Supplier) () -> Arrays.stream(new Object[] {"i44444"}).collect(Collectors.toList())); } } -} \ No newline at end of file +}