From bf79552c902d6638415c78441ddf46771ad5a27b Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Fri, 15 Nov 2024 13:14:33 +0100 Subject: [PATCH] Minimize changes from `2.24.1` --- .../log4j/simple/SimpleLoggerContext.java | 11 +++-- .../logging/log4j/spi/LoggerRegistry.java | 47 ++++++++++--------- .../taglib/Log4jTaglibLoggerContext.java | 13 ++--- .../logging/log4j/tojul/JULLoggerContext.java | 11 +++-- .../logging/slf4j/SLF4JLoggerContext.java | 11 +++-- 5 files changed, 50 insertions(+), 43 deletions(-) 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 a0966bb07bb..55cced06f58 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,12 +102,13 @@ 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; - ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); - if (logger == null) { - logger = createLogger(name, effectiveMessageFactory); - loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + final ExtendedLogger oldLogger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (oldLogger != null) { + return oldLogger; } - return logger; + final ExtendedLogger newLogger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, newLogger); + return loggerRegistry.getLogger(name, effectiveMessageFactory); } 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 9915bc26b12..24eb5417459 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 @@ -20,9 +20,9 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; @@ -39,7 +39,7 @@ @NullMarked public class LoggerRegistry { - private final Map> loggerByNameByMessageFactory = new WeakHashMap<>(); + private final Map> loggerByMessageFactoryByName = new HashMap<>(); private final ReadWriteLock lock = new ReentrantReadWriteLock(); @@ -160,12 +160,10 @@ public LoggerRegistry(@Nullable final MapFactory mapFactory) { requireNonNull(name, "name"); readLock.lock(); try { + final @Nullable Map loggerByMessageFactory = loggerByMessageFactoryByName.get(name); final MessageFactory effectiveMessageFactory = messageFactory != null ? messageFactory : ParameterizedMessageFactory.INSTANCE; - return Optional.of(loggerByNameByMessageFactory) - .map(loggerByNameByMessageFactory -> loggerByNameByMessageFactory.get(effectiveMessageFactory)) - .map(loggerByName -> loggerByName.get(name)) - .orElse(null); + return loggerByMessageFactory == null ? null : loggerByMessageFactory.get(effectiveMessageFactory); } finally { readLock.unlock(); } @@ -179,8 +177,8 @@ public Collection getLoggers(final Collection destination) { requireNonNull(destination, "destination"); readLock.lock(); try { - loggerByNameByMessageFactory.values().stream() - .flatMap(loggerByName -> loggerByName.values().stream()) + loggerByMessageFactoryByName.values().stream() + .flatMap(loggerByMessageFactory -> loggerByMessageFactory.values().stream()) .forEach(destination::add); } finally { readLock.unlock(); @@ -203,7 +201,8 @@ public Collection getLoggers(final Collection destination) { @Deprecated public boolean hasLogger(final String name) { requireNonNull(name, "name"); - return getLogger(name) != null; + final @Nullable T logger = getLogger(name); + return logger != null; } /** @@ -221,7 +220,8 @@ public boolean hasLogger(final String name) { */ public boolean hasLogger(final String name, @Nullable final MessageFactory messageFactory) { requireNonNull(name, "name"); - return getLogger(name, messageFactory) != null; + final @Nullable T logger = getLogger(name, messageFactory); + return logger != null; } /** @@ -237,9 +237,8 @@ public boolean hasLogger(final String name, final Class messageFactoryClass.equals(entry.getKey().getClass())) - .anyMatch(entry -> entry.getValue().containsKey(name)); + return loggerByMessageFactoryByName.getOrDefault(name, Collections.emptyMap()).keySet().stream() + .anyMatch(messageFactory -> messageFactoryClass.equals(messageFactory.getClass())); } finally { readLock.unlock(); } @@ -249,26 +248,30 @@ public boolean hasLogger(final String name, final ClassLogger name and message factory parameters are ignored, those will be obtained from the logger instead. * - * @param name ignored – kept for backward compatibility - * @param messageFactory ignored – kept for backward compatibility + * @param name a logger name + * @param messageFactory a message factory * @param logger a logger instance */ - public void putIfAbsent( - @Nullable final String name, @Nullable final MessageFactory messageFactory, final T logger) { + public void putIfAbsent(final String name, @Nullable final MessageFactory messageFactory, final T logger) { // Check arguments + requireNonNull(name, "name"); requireNonNull(logger, "logger"); // Insert the logger writeLock.lock(); try { - final String loggerName = logger.getName(); - final MessageFactory loggerMessageFactory = logger.getMessageFactory(); - loggerByNameByMessageFactory - .computeIfAbsent(loggerMessageFactory, ignored -> new HashMap<>()) - .putIfAbsent(loggerName, logger); + final MessageFactory effectiveMessageFactory = + messageFactory != null ? messageFactory : ParameterizedMessageFactory.INSTANCE; + loggerByMessageFactoryByName + .computeIfAbsent(name, this::createLoggerRefByMessageFactoryMap) + .putIfAbsent(effectiveMessageFactory, logger); } finally { writeLock.unlock(); } } + + private Map createLoggerRefByMessageFactoryMap(final String ignored) { + return new WeakHashMap<>(); + } } 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 3a30a3ed12d..97a7ab28bbe 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,15 +73,16 @@ 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; - Log4jTaglibLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); - if (logger == null) { - logger = createLogger(name, effectiveMessageFactory); - loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + final Log4jTaglibLogger oldLogger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (oldLogger != null) { + return oldLogger; } - return logger; + final Log4jTaglibLogger newLogger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, newLogger); + return loggerRegistry.getLogger(name, effectiveMessageFactory); } - private Log4jTaglibLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) { + private Log4jTaglibLogger createLogger(final String name, final MessageFactory messageFactory) { final LoggerContext loggerContext = LogManager.getContext(false); final ExtendedLogger delegateLogger = loggerContext.getLogger(name, messageFactory); return new Log4jTaglibLogger(delegateLogger, name, delegateLogger.getMessageFactory()); 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 675ea2cc900..6c18a83d485 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,12 +52,13 @@ 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; - ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); - if (logger == null) { - logger = createLogger(name, effectiveMessageFactory); - loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + final ExtendedLogger oldLogger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (oldLogger != null) { + return oldLogger; } - return logger; + final ExtendedLogger newLogger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, newLogger); + return loggerRegistry.getLogger(name, effectiveMessageFactory); } 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 e14b0471491..50160e9063c 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,12 +45,13 @@ 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; - ExtendedLogger logger = loggerRegistry.getLogger(name, effectiveMessageFactory); - if (logger == null) { - logger = createLogger(name, effectiveMessageFactory); - loggerRegistry.putIfAbsent(name, effectiveMessageFactory, logger); + final ExtendedLogger oldLogger = loggerRegistry.getLogger(name, effectiveMessageFactory); + if (oldLogger != null) { + return oldLogger; } - return logger; + final ExtendedLogger newLogger = createLogger(name, effectiveMessageFactory); + loggerRegistry.putIfAbsent(name, effectiveMessageFactory, newLogger); + return loggerRegistry.getLogger(name, effectiveMessageFactory); } private static ExtendedLogger createLogger(final String name, @Nullable final MessageFactory messageFactory) {