Skip to content

Commit

Permalink
Remove computeIfAbsent
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Nov 13, 2024
1 parent 780d88f commit 8963f70
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -254,8 +252,6 @@ public boolean hasLogger(final String name, final Class<? extends MessageFactory
* @param name ignored – kept for backward compatibility
* @param messageFactory ignored – kept for backward compatibility
* @param logger a logger instance
* @deprecated As of version {@code 2.25.0}, planned to be removed!
* Use {@link #computeIfAbsent(String, MessageFactory, BiFunction)} instead.
*/
public void putIfAbsent(
@Nullable final String name, @Nullable final MessageFactory messageFactory, final T logger) {
Expand All @@ -275,66 +271,4 @@ public void putIfAbsent(
writeLock.unlock();
}
}

/**
* Returns an existent logger or creates a new one.
*
* @param name The logger name.
* @param messageFactory The message factory to use.
* @param loggerSupplier Used to create the logger if it does not exist already
* @return A logger
* @since 2.25.0
*/
public T computeIfAbsent(
final String name,
final MessageFactory messageFactory,
final BiFunction<String, MessageFactory, T> 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<String, T> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8963f70

Please sign in to comment.