gpt4 book ai didi

java - Log4j2 - ServletContextListener 中的 ThreadLocal 内存泄漏

转载 作者:行者123 更新时间:2023-11-28 22:18:34 26 4
gpt4 key购买 nike

关闭 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.

只有当我尝试在 ServletContextListener 实现中的 contextInitialized 或 contextDestroyed 方法中记录某些内容时,它才会出现。一旦 Tomcat 启动,日志记录在应用程序的其余部分工作正常。我注意到 StringBuilder 的值始终是最后记录的条目。在这种情况下,以下代码生成日志:

@Override
public void contextInitialized(ServletContextEvent arg0)
{
Logger logger = LogManager.getLogger("AppLogger");
logger.error("Error log.");
}

经过几个小时的调查(包括 SO),我仍然找不到解释。这是一个 Log4j2 初始化问题吗?这是我应该报告的错误吗?

我在 Eclipse 4.5 中使用 Tomcat 8.0

log4j-core-2.4、log4j-api-2.4、log4j-web-2.4 在类路径中。

web.xml 在'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 在'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>

最佳答案

这与 ThreadLocal 有关 introduced in Log4j 2.4 .这是发布后发现的,will be fixed在下一个版本中,将是 2.4.1 或 2.5(我们希望这会很快,在一两周内)。

关于java - Log4j2 - ServletContextListener 中的 ThreadLocal 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com