Reputation: 658
I get the following error when shutting down Tomcat :
SEVERE: The web application [App] created a ThreadLocal with key of type [org.apache.logging.log4j.core.layout.PatternLayout$1] (value [org.apache.logging.log4j.core.layout.PatternLayout$1@14391aaf]) and a value of type [java.lang.StringBuilder] (value [2015-09-30 14:22:27.832 [localhost-startStop-1] ERROR AppLogger - Error log. ]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
It appears only if I try to log something inside a contextInitialized or contextDestroyed method in a ServletContextListener implementation. Logging works fine in the rest of the app once Tomcat has started. I noticed the value of the StringBuilder is always the last entry logged. In this case, the following code generates the log :
@Override
public void contextInitialized(ServletContextEvent arg0)
{
Logger logger = LogManager.getLogger("AppLogger");
logger.error("Error log.");
}
After several hours of investigation (including SO), I still can't find an explanation. Is this a Log4j2 initialization problem? Is this a bug I should report?
I'm using Tomcat 8.0 in Eclipse 4.5
log4j-core-2.4, log4j-api-2.4, log4j-web-2.4 are in classpath.
web.xml is in 'WEB-INF'
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>log4jContextName</param-name>
<param-value>App</param-value>
</context-param>
</web-app>
log4j2.xml is in 'src'
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!-- http://logging.apache.org/log4j/2.x/manual/configuration.html -->
<Appenders>
<File name="File" fileName="/home/user/app.log">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Console name="Console">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="AppLogger" level="ALL">
<AppenderRef ref="File"/>
</Logger>
<Root level="ALL">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Upvotes: 1
Views: 2103
Reputation: 36754
This is related to a ThreadLocal introduced in Log4j 2.4. This was discovered after the release and will be fixed in the next release, which will be either 2.4.1 or 2.5 (we hope this will be soon, in a week or two).
Upvotes: 1