This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 828c1f7c119379e4dbdb831bd88c55eada5db820 Author: Piotr P. Karwasz <[email protected]> AuthorDate: Tue Nov 14 11:34:38 2023 +0100 Add default value to `newCheckedInstanceOfProperty` --- .../org/apache/logging/log4j/util/LoaderUtil.java | 1 + .../logging/log4j/core/async/DisruptorUtil.java | 31 ++++++++++------------ .../core/impl/ContextDataInjectorFactory.java | 12 ++++----- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java index ec1299f326..638317a2b5 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LoaderUtil.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Objects; +import java.util.function.Supplier; import org.apache.logging.log4j.spi.LoggingSystemProperty; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java index 71c1261f65..9f3e390221 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DisruptorUtil.java @@ -16,11 +16,10 @@ */ package org.apache.logging.log4j.core.async; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; - import com.lmax.disruptor.ExceptionHandler; import com.lmax.disruptor.WaitStrategy; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.impl.Log4jPropertyKey; import org.apache.logging.log4j.core.util.Integers; @@ -29,6 +28,8 @@ import org.apache.logging.log4j.util.LoaderUtil; import org.apache.logging.log4j.util.PropertiesUtil; import org.apache.logging.log4j.util.PropertyKey; +import static org.apache.logging.log4j.core.impl.Log4jPropertyKey.ASYNC_CONFIG_EXCEPTION_HANDLER_CLASS_NAME; +import static org.apache.logging.log4j.core.impl.Log4jPropertyKey.ASYNC_LOGGER_EXCEPTION_HANDLER_CLASS_NAME; import static org.apache.logging.log4j.util.Constants.isThreadLocalsEnabled; /** @@ -84,31 +85,27 @@ final class DisruptorUtil { } static ExceptionHandler<RingBufferLogEvent> getAsyncLoggerExceptionHandler() { - ExceptionHandler<RingBufferLogEvent> handler = null; try { - handler = - LoaderUtil.newCheckedInstanceOfProperty(Log4jPropertyKey.ASYNC_LOGGER_EXCEPTION_HANDLER_CLASS_NAME, ExceptionHandler.class); + return LoaderUtil.newCheckedInstanceOfProperty( + ASYNC_LOGGER_EXCEPTION_HANDLER_CLASS_NAME, + ExceptionHandler.class, + AsyncLoggerDefaultExceptionHandler::new); } catch (final ReflectiveOperationException e) { LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e); + return new AsyncLoggerDefaultExceptionHandler(); } - if (handler != null) { - return handler; - } - return new AsyncLoggerDefaultExceptionHandler(); } static ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> getAsyncLoggerConfigExceptionHandler() { - ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> handler = null; try { - handler = LoaderUtil.newCheckedInstanceOfProperty( - Log4jPropertyKey.ASYNC_CONFIG_EXCEPTION_HANDLER_CLASS_NAME, ExceptionHandler.class); + return LoaderUtil.newCheckedInstanceOfProperty( + ASYNC_CONFIG_EXCEPTION_HANDLER_CLASS_NAME, + ExceptionHandler.class, + AsyncLoggerConfigDefaultExceptionHandler::new); } catch (final ReflectiveOperationException e) { LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e); + return new AsyncLoggerConfigDefaultExceptionHandler(); } - if (handler != null) { - return handler; - } - return new AsyncLoggerConfigDefaultExceptionHandler(); } /** diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java index ed014d9d52..13e16a8c87 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataInjectorFactory.java @@ -65,17 +65,15 @@ public class ContextDataInjectorFactory { * @see ContextDataInjector */ public static ContextDataInjector createInjector() { - ContextDataInjector injector = null; try { - injector = - LoaderUtil.newCheckedInstanceOfProperty(Log4jPropertyKey.THREAD_CONTEXT_DATA_INJECTOR_CLASS_NAME, ContextDataInjector.class); + return LoaderUtil.newCheckedInstanceOfProperty( + Log4jPropertyKey.THREAD_CONTEXT_DATA_INJECTOR_CLASS_NAME, + ContextDataInjector.class, + ContextDataInjectorFactory::createDefaultInjector); } catch (final ReflectiveOperationException e) { StatusLogger.getLogger().warn("Could not create ContextDataInjector: {}", e.getMessage(), e); + return createDefaultInjector(); } - if (injector != null) { - return injector; - } - return createDefaultInjector(); } private static ContextDataInjector createDefaultInjector() {
