title: Dynamically add appender with slf4j and log4j2
body: <p>I want to dynamically create an appender and add it to a logger. However, this seems not to be possible with slf4j. I can add my appender to a log4j logger but then I fail to retrieve the logger with the slf4j LoggerFactoy.</p>
  
  <p>What I want to do: I create a test class (not a jUnit test) and pass a logger in the constructor for the test class to use. Every instance of the test class needs it's own logger and appender that saves the log so it can be later used in an HTML report.</p>
  
  <p>What I tried (for simplicity I created a jUnit test):</p>
  
  <pre><code>  import static org.junit.Assert.assertEquals;
  
    import java.util.LinkedList;
    import java.util.List;
  
    import org.apache.logging.log4j.core.LogEvent;
    import org.junit.Test;
    import org.slf4j.helpers.Log4jLoggerFactory;
  
    import ch.fides.fusion.logging.ListAppender;
  
    public class ListAppenderTest {
  
        @Test
        public void test() {
  
            String testName = "test1";
  
            // the log messages are to be inserted in this list
            List&lt;LogEvent&gt; testLog = new LinkedList&lt;&gt;();
  
            // create log4j logger
            org.apache.logging.log4j.core.Logger log4jlogger = (org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
                                            .getLogger("Test:" + testName);
  
            // create appender and add it to the logger
            ListAppender listAppender = new ListAppender("Test:" + testName + ":MemoryAppender", testLog);
            log4jlogger.addAppender(listAppender);
  
            // get the slf4j logger
            org.slf4j.helpers.Log4jLoggerFactory loggerFactory = new Log4jLoggerFactory();
            org.slf4j.Logger testLogger = loggerFactory.getLogger("Test:" + testName);
  
            // test it
            final String TEST_MESSAGE = "test message";
            testLogger.info(TEST_MESSAGE);
  
            assertEquals(1, testLog.size());
            LogEvent logEvent = testLog.get(0);
            assertEquals(TEST_MESSAGE, logEvent.getMessage().getFormattedMessage() );
        }
  
    }
  </code></pre>
  
  <p>and this is my very basic appender:</p>
  
  <pre><code> package ch.fides.fusion.logging;
  
    import java.util.List;
  
    import org.apache.logging.log4j.core.LogEvent;
    import org.apache.logging.log4j.core.appender.AbstractAppender;
  
    public class ListAppender extends AbstractAppender {
  
        private final List&lt;LogEvent&gt; log;
  
        public ListAppender(String name, List&lt;LogEvent&gt; testLog) {
            super(name, null, null);
            this.log = testLog;
        }
  
        @Override
        public void append(LogEvent logEvent) {
            log.add(new TestLogEvent(logEvent));
        }
  
    }
  </code></pre>
  
  <p>What can I do to get this to work? Maybe I am approaching this from the wrong angle but I would like to avoid creating my own logger class. Any help is greatly appreciated.</p>