Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3641

Logger.addAppender replaces LoggerConfig

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.17.2
    • None
    • Core
    • None

    Description

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

       

          public void addAppender(final Appender appender) {
              privateConfig.config.addLoggerAppender(this, appender);
          }
      

      AbstractConfiguration:

          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();
              }
          }
      

      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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            rpopma Remko Popma
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: