ramanathan1504 commented on code in PR #4419:
URL: https://github.com/apache/logging-log4j2/pull/4419#discussion_r4409326636


##########
log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java:
##########
@@ -959,17 +959,30 @@ public synchronized void addLoggerAppender(
             return;
         }
         final String loggerName = logger.getName();
-        appenders.putIfAbsent(appender.getName(), appender);
+        final Appender registered = appenders.putIfAbsent(appender.getName(), 
appender);
+        // The configuration owns a single appender per name: when another 
instance is already registered under
+        // this name, attach that one instead of an instance the configuration 
would never start or stop.
+        final Appender effective = registered == null ? appender : registered;
+        if (effective != appender) {
+            LOGGER.warn(
+                    "An appender named '{}' is already registered; attaching 
the registered instance to logger '{}' instead of the given one",
+                    appender.getName(),
+                    loggerName);
+        }
         final LoggerConfig lc = getLoggerConfig(loggerName);
         if (lc.getName().equals(loggerName)) {
-            lc.addAppender(appender, null, null);
+            lc.addAppender(effective, null, null);
         } else {
             final LoggerConfig nlc = new LoggerConfig(loggerName, 
lc.getLevel(), lc.isAdditive());
-            nlc.addAppender(appender, null, null);
+            nlc.addAppender(effective, null, null);
             nlc.setParent(lc);
-            loggerConfigs.putIfAbsent(loggerName, nlc);
-            setParents();
-            logger.getContext().updateLoggers();
+            final LoggerConfig existing = 
loggerConfigs.putIfAbsent(loggerName, nlc);
+            if (existing == null) {
+                setParents();
+                logger.getContext().updateLoggers();
+            } else {
+                existing.addAppender(effective, null, null);

Review Comment:
   This branch and the `existing.addFilter(filter)` and 
`existing.setAdditive(additive)` branches below have no test. Removing any of 
them keeps every test green. A `LoggerConfig` added with `addLogger` under a 
key that differs from its name reaches them, for example:
   
   ```java
   final LoggerConfig other = new LoggerConfig("other", Level.INFO, true);
   config.addLogger("mismatch", other);
   config.addLoggerFilter(context.getLogger("mismatch"), filter);
   assertThat(other.hasFilter()).isTrue();
   ```



##########
src/changelog/.2.x.x/3155_AbstractConfiguration_logger_mutators.xml:
##########
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+           https://logging.apache.org/xml/ns
+           https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+    <issue id="3155" 
link="https://github.com/apache/logging-log4j2/issues/3155"/>
+    <description format="asciidoc">
+        Fix `addLoggerAppender()` attaching an unregistered appender when its 
name is already registered, and `addLoggerFilter()`/`setLoggerAdditive()` 
failing on `null` arguments.

Review Comment:
   Can the entry be one short line with what the user sees?
   
   ```suggestion
           Fix `Logger.addAppender()` attaching an unregistered appender.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to