title: Log4j2 AsyncAppender thread does not die on tomcat undeploy causing memory leak
body: <p>The stack I am using at the moment is:</p>
  
  <ul>
  <li>log4j2 rc1</li>
  <li>spring 3.2 core and web</li>
  <li>tomcat 7.0.47</li>
  <li>java 1.6.0_45</li>
  <li>Windows 7</li>
  </ul>
  
  <p>I don't have the ability to alter the tomcat version or java version and I would prefer not to alter the log4j version and spring version.</p>
  
  <p>Essentially, when I undeploy my webapp I receieve a SEVERE warning saying:</p>
  
  <pre><code>SEVERE: The web application [/MyApp] appears to have started a thread named
  [AsyncAppender-AsyncFile] but has failed to stop it. This is very likely to 
  create a memory leak
  </code></pre>
  
  <p>I can confirm that it does create a memory leak and this is what I am trying to fix.</p>
  
  <p>So far I have attempted to create a custom ServletContextListener which contains the following code:</p>
  
  <pre><code>@Override
  public void contextDestroyed(ServletContextEvent event) {
      System.out.println("Shutting down logger");
      try {
          ((Log4jWebSupport) event.getServletContext().getAttribute(
              Log4jWebSupport.SUPPORT_ATTRIBUTE)).clearLoggerContext();
          ((LifeCycle) LogManager.getContext()).stop();
      } catch (Exception e) {
  
      }
  }
  </code></pre>
  
  <p>None of these two lines seems to fix the problem, however I can confirm that this code is being executed due to my sysout statement appearing in the tomcat console.</p>
  
  <p>I am using log4j2 through an interceptor which I am setting up using Spring</p>
  
  <pre><code>&lt;mvc:interceptors&gt;
      &lt;mvc:interceptor&gt;
          &lt;mvc:mapping path="/**" /&gt;
          &lt;ref bean="LoggingInterceptor"/&gt;
      &lt;/mvc:interceptor&gt;
  &lt;/mvc:interceptors&gt;
  &lt;bean id="LoggingInterceptor" class="MyLoggerClass"&gt;
  </code></pre>
  
  <p>The interceptor works correctly and the logs appear where I expect them to. My implementation of the logger is:</p>
  
  <pre><code>private static Logger log = LogManager.getLogger("MetricLogger");
  
  public void log(LogPayload payload) {
      if(payload != null){
          log.info(payload.get());
      }
  }
  </code></pre>
  
  <p>Where <code>LogPayload.get()</code> returns a String.</p>
  
  <p>As I am using the logging facility across multiple webapps I have created a separate jar file containing this and the classes recording the measurements. I have included this using maven and I compile it into the final war file I deploy to tomcat. This war file is included on a per app basis and is not included in the global tomcat/lib folder.</p>
  
  <p>Does anyone have any insight as to why I get my memory leak issue and what the possible solutions are to fixing this?</p>
  
  <p>Many thanks for your help and please let me know if you need further information.</p>