我有一个带有单个ServletContextListener的Java Web应用程序,该应用程序在应用程序启动和关闭时都创建并使用log4j Logger。
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
getLogger().info("My context is initialized");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
getLogger().info("My context is destroyed");
}
private Logger getLogger() {
return LogManager.getLogger();
}
}
Log4j配置文件是在web.xml中定义的,而且我正在使用log4j发行版中提供的log4j-web jar。
在启动过程中,日志消息已正确打印,但是在关闭时,找不到我的配置文件,因此无法配置记录器,并显示以下错误消息:
2018-12-11 16:56:44,722 localhost-startStop-2 ERROR No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
我创建了一个示例项目来重现此问题:
https://github.com/katanagari7c1/tomcat-shutdown-test
可以通过使用此应用程序启动/停止Tomcat来简单地复制它。
有关如何解决此问题的任何线索?我想念什么吗?