Remko Popma created LOG4J2-3641:
-----------------------------------

             Summary: Logger.addAppender replaces LoggerConfig
                 Key: LOG4J2-3641
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3641
             Project: Log4j 2
          Issue Type: Bug
          Components: Core
    Affects Versions: 2.17.2
            Reporter: Remko Popma


Logger.addAppender(Appender) in core effectively removes all appenders and ends 
up with just the specified Appender.

 
{code:java}
    public void addAppender(final Appender appender) {
        privateConfig.config.addLoggerAppender(this, appender);
    }
{code}
AbstractConfiguration:
{code:java}
    public synchronized void addLoggerAppender(final 
org.apache.logging.log4j.core.Logger logger,
            final Appender appender) {
        if (appender == null || logger == null) {
            return;
        }
        final String loggerName = logger.getName();
        appenders.putIfAbsent(appender.getName(), appender);
        final LoggerConfig lc = getLoggerConfig(loggerName);
        if (lc.getName().equals(loggerName)) {
            lc.addAppender(appender, null, null);
        } else {
            final LoggerConfig nlc = new LoggerConfig(loggerName, 
lc.getLevel(), lc.isAdditive());
            nlc.addAppender(appender, null, null);
            nlc.setParent(lc);
            loggerConfigs.putIfAbsent(loggerName, nlc);
            setParents();
            logger.getContext().updateLoggers();
        }
    }
{code}
Most Loggers have a shared LoggerConfig ("root").
The LoggerConfig returned by {{getLoggerConfig(loggerName)}} is that root 
LoggerConfig - which has the Appenders in the original configuration.

The above code creates a new LoggerConfig, adds the specified Appender to that 
LoggerConfig, and replaces Logger's reference to point to this new LoggerConfig 
instead of the shared "root" LoggerConfig. Effectively the old Appenders are 
removed from this logger.

*Solution*

The old Appenders in the "root" LoggerConfig should be copied into the new 
LoggerConfig, in addition to the specified Appender.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to