The stack I am using at the moment is:
- log4j2 rc1
- spring 3.2 core and web
- tomcat 7.0.47
- java 1.6.0_45
- Windows 7
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.
Essentially, when I undeploy my webapp I receieve a SEVERE warning saying:
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
I can confirm that it does create a memory leak and this is what I am trying to fix.
So far I have attempted to create a custom ServletContextListener which contains the following 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) {
}
}
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.
I am using log4j2 through an interceptor which I am setting up using Spring
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<ref bean="LoggingInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean id="LoggingInterceptor" class="MyLoggerClass">
The interceptor works correctly and the logs appear where I expect them to. My implementation of the logger is:
private static Logger log = LogManager.getLogger("MetricLogger");
public void log(LogPayload payload) {
if(payload != null){
log.info(payload.get());
}
}
Where LogPayload.get()
returns a String.
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.
Does anyone have any insight as to why I get my memory leak issue and what the possible solutions are to fixing this?
Many thanks for your help and please let me know if you need further information.