diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java index 4663cbc9c6c..a0966bb07bb 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/simple/SimpleLoggerContext.java @@ -102,7 +102,12 @@ public ExtendedLogger getLogger(final String name) { public ExtendedLogger getLogger(final String name, @Nullable final MessageFactory messageFactory) { final MessageFactory effectiveMessageFactory = messageFactory != null ? messageFactory : DEFAULT_MESSAGE_FACTORY; - return loggerRegistry.computeIfAbsent(name, effectiveMessageFactory, this::createLogger); + ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (logger == null) { + logger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + } + return logger; } private ExtendedLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) { diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java index 9abc2c4b6f4..9915bc26b12 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggerRegistry.java @@ -28,10 +28,8 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.function.BiFunction; import org.apache.logging.log4j.message.MessageFactory; import org.apache.logging.log4j.message.ParameterizedMessageFactory; -import org.apache.logging.log4j.status.StatusLogger; import org.jspecify.annotations.NullMarked; import org.jspecify.annotations.Nullable; @@ -254,8 +252,6 @@ public boolean hasLogger(final String name, final Class loggerSupplier) { - - // Check arguments - requireNonNull(name, "name"); - requireNonNull(messageFactory, "messageFactory"); - requireNonNull(loggerSupplier, "loggerSupplier"); - - // Read lock fast path: See if logger already exists - @Nullable T logger = getLogger(name, messageFactory); - if (logger != null) { - return logger; - } - - // Write lock slow path: Insert the logger - writeLock.lock(); - try { - - // See if the logger is created by another thread in the meantime - final Map loggerByName = - loggerByNameByMessageFactory.computeIfAbsent(messageFactory, ignored -> new HashMap<>()); - logger = loggerByName.get(name); - if (logger != null) { - return logger; - } - - // Create the logger - logger = loggerSupplier.apply(name, messageFactory); - - // Report message factory mismatches, if there is any - final MessageFactory loggerMessageFactory = logger.getMessageFactory(); - if (!loggerMessageFactory.equals(messageFactory)) { - StatusLogger.getLogger() - .error( - "Newly registered logger with name `{}` and message factory `{}`, is requested to be associated with a different message factory: `{}`.\n" - + "Effectively the message factory of the logger will be used and the other one will be ignored.\n" - + "This generally hints a problem at the logger context implementation.\n" - + "Please report this using the Log4j project issue tracker.", - name, - loggerMessageFactory, - messageFactory); - } - - // Insert the logger - loggerByName.put(name, logger); - return logger; - } finally { - writeLock.unlock(); - } - } } diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/package-info.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/package-info.java index 3b6b5c25857..1748932083d 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/package-info.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/package-info.java @@ -19,7 +19,7 @@ * API classes. */ @Export -@Version("2.25.0") +@Version("2.24.1") package org.apache.logging.log4j.spi; import org.osgi.annotation.bundle.Export; diff --git a/log4j-taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java b/log4j-taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java index 5d25ff6d89d..3a30a3ed12d 100644 --- a/log4j-taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java +++ b/log4j-taglib/src/main/java/org/apache/logging/log4j/taglib/Log4jTaglibLoggerContext.java @@ -73,7 +73,12 @@ public Log4jTaglibLogger getLogger(final String name) { public Log4jTaglibLogger getLogger(final String name, @Nullable final MessageFactory messageFactory) { final MessageFactory effectiveMessageFactory = messageFactory != null ? messageFactory : DEFAULT_MESSAGE_FACTORY; - return loggerRegistry.computeIfAbsent(name, effectiveMessageFactory, this::createLogger); + Log4jTaglibLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (logger == null) { + logger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + } + return logger; } private Log4jTaglibLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) { diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java index 9f92dca4931..675ea2cc900 100644 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java +++ b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java @@ -52,7 +52,12 @@ public ExtendedLogger getLogger(final String name) { public ExtendedLogger getLogger(final String name, @Nullable final MessageFactory messageFactory) { final MessageFactory effectiveMessageFactory = messageFactory != null ? messageFactory : DEFAULT_MESSAGE_FACTORY; - return loggerRegistry.computeIfAbsent(name, effectiveMessageFactory, JULLoggerContext::createLogger); + ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (logger == null) { + logger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + } + return logger; } private static ExtendedLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) { diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java index ba2a1d48189..e14b0471491 100644 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java +++ b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java @@ -45,7 +45,12 @@ public ExtendedLogger getLogger(final String name) { public ExtendedLogger getLogger(final String name, @Nullable final MessageFactory messageFactory) { final MessageFactory effectiveMessageFactory = messageFactory != null ? messageFactory : DEFAULT_MESSAGE_FACTORY; - return loggerRegistry.computeIfAbsent(name, effectiveMessageFactory, SLF4JLoggerContext::createLogger); + ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (logger == null) { + logger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + } + return logger; } private static ExtendedLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) {