Skip to content

Commit

Permalink
Minimize changes from 2.24.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Nov 15, 2024
1 parent 041e5eb commit bf79552
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@
@NullMarked
public class LoggerRegistry<T extends ExtendedLogger> {

private final Map<MessageFactory, Map<String, T>> loggerByNameByMessageFactory = new WeakHashMap<>();
private final Map<String, Map<MessageFactory, T>> loggerByMessageFactoryByName = new HashMap<>();

private final ReadWriteLock lock = new ReentrantReadWriteLock();

Expand Down Expand Up @@ -160,12 +160,10 @@ public LoggerRegistry(@Nullable final MapFactory<T> mapFactory) {
requireNonNull(name, "name");
readLock.lock();
try {
final @Nullable Map<MessageFactory, T> 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();
}
Expand All @@ -179,8 +177,8 @@ public Collection<T> getLoggers(final Collection<T> 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();
Expand All @@ -203,7 +201,8 @@ public Collection<T> getLoggers(final Collection<T> destination) {
@Deprecated
public boolean hasLogger(final String name) {
requireNonNull(name, "name");
return getLogger(name) != null;
final @Nullable T logger = getLogger(name);
return logger != null;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -237,9 +237,8 @@ public boolean hasLogger(final String name, final Class<? extends MessageFactory
requireNonNull(messageFactoryClass, "messageFactoryClass");
readLock.lock();
try {
return loggerByNameByMessageFactory.entrySet().stream()
.filter(entry -> 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();
}
Expand All @@ -249,26 +248,30 @@ public boolean hasLogger(final String name, final Class<? extends MessageFactory
* Registers the provided logger.
* <b>Logger name and message factory parameters are ignored</b>, 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<MessageFactory, T> createLoggerRefByMessageFactoryMap(final String ignored) {
return new WeakHashMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bf79552

Please sign in to comment.