From 044732df2951b35a1fb11e2c5cbbc379cb9be885 Mon Sep 17 00:00:00 2001 From: Vasily Pelikh <2010720+vpelikh@users.noreply.github.com> Date: Tue, 4 Aug 2026 13:53:56 +0300 Subject: [PATCH] Remove `LoggerContext.checkMessageFactory` The warning was dead code: `LoggerRegistry.computeIfAbsent` always returns a logger matching the requested message factory, so the mismatch condition could never fire after the message factory-namespaced registry port (#4157). Remove it and pin the behavior with a regression test asserting no warning is emitted when a logger is re-requested with a different message factory. --- .../apache/logging/log4j/core/LoggerTest.java | 14 ++++- .../logging/log4j/core/LoggerContext.java | 51 +++---------------- 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/LoggerTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/LoggerTest.java index 06a393f5137..d9bc269237a 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/LoggerTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/LoggerTest.java @@ -52,6 +52,7 @@ import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.message.StringFormatterMessageFactory; import org.apache.logging.log4j.message.StructuredDataMessage; +import org.apache.logging.log4j.status.StatusLogger; import org.awaitility.Awaitility; import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Tag; @@ -121,7 +122,7 @@ public void basicFlow() { @Test public void builder() { - final int currentLine = 124; + final int currentLine = 125; logger.atDebug().withLocation().log("Hello"); final Marker marker = MarkerManager.getMarker("test"); logger.atError().withMarker(marker).log("Hello {}", "John"); @@ -419,6 +420,17 @@ public void getLogger_String_MessageFactoryMismatchNull(final TestInfo testInfo) events.get(0).getMessage().getFormattedMessage()); } + @Test + public void getLogger_String_MessageFactoryMismatchProducesNoWarning(final TestInfo testInfo) { + final String name = testInfo.getTestMethod().map(Method::getName).orElseThrow(AssertionError::new); + testMessageFactoryMismatch(name, StringFormatterMessageFactory.INSTANCE, new ReusableMessageFactory()); + testMessageFactoryMismatch(name + "Null", StringFormatterMessageFactory.INSTANCE, null); + final boolean mismatchWarning = StatusLogger.getLogger().getStatusData().stream() + .map(data -> data.getMessage().getFormattedMessage()) + .anyMatch(message -> message.contains("created with the message factory")); + assertFalse(mismatchWarning, "The message factory mismatch warning should not be emitted"); + } + @Test public void mdc() { ThreadContext.put("TestYear", "2010"); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 1acbff7e8df..ea16b98e595 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -63,13 +63,11 @@ import org.apache.logging.log4j.plugins.di.Key; import org.apache.logging.log4j.plugins.di.spi.ConfigurableInstanceFactoryPostProcessor; import org.apache.logging.log4j.plugins.util.OrderedComparator; -import org.apache.logging.log4j.spi.ExtendedLogger; import org.apache.logging.log4j.spi.LoggerContextFactory; import org.apache.logging.log4j.spi.LoggerContextShutdownAware; import org.apache.logging.log4j.spi.LoggerContextShutdownEnabled; import org.apache.logging.log4j.spi.LoggerRegistry; import org.apache.logging.log4j.spi.Terminable; -import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.Lazy; import org.apache.logging.log4j.util.ServiceLoaderUtil; import org.jspecify.annotations.Nullable; @@ -182,38 +180,6 @@ public LoggerContext( this(contextName, externalContext, fileToUri(configLocation), instanceFactory); } - /** - * Checks that the message factory a logger was created with is the same as the given messageFactory. If they are - * different log a warning to the {@linkplain StatusLogger}. A null MessageFactory translates to the default - * MessageFactory. - * - * @param logger The logger to check - * @param messageFactory The message factory to check. - */ - private void checkMessageFactory(final ExtendedLogger logger, final MessageFactory messageFactory) { - final String name = logger.getName(); - final MessageFactory loggerMessageFactory = logger.getMessageFactory(); - final MessageFactory currentMessageFactory = defaultMessageFactory; - if (messageFactory != null && !loggerMessageFactory.equals(messageFactory)) { - StatusLogger.getLogger() - .warn( - "The Logger {} was created with the message factory {} and is now requested with the " - + "message factory {}, which may create log events with unexpected formatting.", - name, - loggerMessageFactory, - messageFactory); - } else if (messageFactory == null && loggerMessageFactory != currentMessageFactory) { - StatusLogger.getLogger() - .warn( - "The Logger {} was created with the message factory {} and is now requested with a null " - + "message factory (defaults to {}), which may create log events with unexpected " - + "formatting.", - name, - loggerMessageFactory, - currentMessageFactory.getClass().getName()); - } - } - public PropertyEnvironment getEnvironment() { return environment; } @@ -563,19 +529,16 @@ public Collection getLoggers() { } /** - * Obtains a Logger from the Context. + * Obtains a logger from the context. * - * @param name The name of the Logger to return. - * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the - * logger but will log a warning if mismatched. - * @return The Logger. + * @param name a logger name + * @param messageFactory a message factory to associate the logger with + * @return a logger matching the given name and message factory */ @Override - public Logger getLogger(final String name, final MessageFactory messageFactory) { + public Logger getLogger(final String name, final @Nullable MessageFactory messageFactory) { final MessageFactory actualMessageFactory = messageFactory != null ? messageFactory : defaultMessageFactory; - final Logger logger = loggerRegistry.computeIfAbsent(name, actualMessageFactory, this::newLogger); - checkMessageFactory(logger, actualMessageFactory); - return logger; + return loggerRegistry.computeIfAbsent(name, actualMessageFactory, this::newLogger); } /** @@ -617,7 +580,7 @@ public boolean hasLogger(final String name) { * @return True if the Logger exists, false otherwise. */ @Override - public boolean hasLogger(final String name, final MessageFactory messageFactory) { + public boolean hasLogger(final String name, final @Nullable MessageFactory messageFactory) { return loggerRegistry.hasLogger(name, messageFactory); }