You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ra...@apache.org on 2026/09/23 19:27:48 UTC

(logging-log4j2) branch main updated: Port `MessageFactory`-namespaced logger registry from `2.x` (#4240)

This is an automated email from the ASF dual-hosted git repository.

ramanathan1504 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/main by this push:
     new 137a2a476e Port `MessageFactory`-namespaced logger registry from `2.x` (#4240)
137a2a476e is described below

commit 137a2a476ed25aa0231699c8fed1f9bd792feb87
Author: Vasily Pelikh <Ja...@gmail.com>
AuthorDate: Wed Sep 23 22:27:41 2026 +0300

    Port `MessageFactory`-namespaced logger registry from `2.x` (#4240)
---
 .../apache/logging/log4j/core/LoggerContext.java   | 51 +++-------------------
 .../2936_logger_context_check_message_factory.xml  | 12 +++++
 2 files changed, 19 insertions(+), 44 deletions(-)

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 1acbff7e8d..ea16b98e59 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.InstanceFactory;
 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 class LoggerContext extends AbstractLifeCycle
         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 class LoggerContext extends AbstractLifeCycle
     }
 
     /**
-     * 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 class LoggerContext extends AbstractLifeCycle
      * @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);
     }
 
diff --git a/src/changelog/.3.x.x/2936_logger_context_check_message_factory.xml b/src/changelog/.3.x.x/2936_logger_context_check_message_factory.xml
new file mode 100644
index 0000000000..82bff51857
--- /dev/null
+++ b/src/changelog/.3.x.x/2936_logger_context_check_message_factory.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns="https://logging.apache.org/xml/ns"
+       xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+       type="removed">
+  <issue id="2936" link="https://github.com/apache/logging-log4j2/pull/2936"/>
+  <description format="asciidoc">
+    Removes the now-dead `checkMessageFactory` machinery from `LoggerContext` now that the
+    logger registry keys loggers by their message factory. Requests for a logger with a different
+    factory now return a distinct, correctly-bound logger instead of the old logger plus a warning.
+  </description>
+</entry>
\ No newline at end of file