从servlet上下文记录销毁事件 [英] Logging from servlet context destroyed event
问题描述
在我基于Servlet的应用程序中,我想记录启动和关闭的事件。
Within my Servlet-based application, I would like to log events for startup and shutdown.
我试图实现 ServletContextListener
这样做的接口:
I've tried to implement the ServletContextListener
interface to do this:
public class DiagnosticListener
implements ServletContextListener {
private static final Logger LOG = LogManager.getLogger(DiagnosticListener.class);
@Override
public void contextInitialized( final ServletContextEvent sce ) {
LOG.info("Context initialized.");
}
@Override
public void contextDestroyed( final ServletContextEvent sce ) {
LOG.info("Context destroyed.");
}
}
初始化事件按预期记录,但已销毁事件永远不会出现我假设这与log4j2如何使用类似的侦听器管理其生命周期有关,在此事件期间,日志记录基础结构不再可用。
The initialized event is logged as expected, but the destroyed event never appears. I am assuming this is to do with how log4j2 manages its lifecycle using a similar listener, that logging infrastructure is no longer available during this event.
有没有办法记录应用程序被关闭的事件?
Is there a way to log an event for the application being shut down?
推荐答案
我们与Logback的类似问题发生冲突。
您必须编写自己的web.xml来解决这个问题,因为没有其他选择来定义侦听器顺序。
We clashed against a similar issue with Logback. You have to write your own web.xml to fix that, because there's no alternatives to define listeners order.
我们使用以下命令禁用了LogbackServletContextListener:
We disabled the LogbackServletContextListener with:
<context-param>
<param-name>logbackDisableServletContainerInitializer</param-name>
<param-value>true</param-value>
</context-param>
然后手动添加LogbackServletContextListener作为第一个监听器:
then add the LogbackServletContextListener by hand as the first listener:
<listener>
<listener-class>ch.qos.logback.classic.servlet.LogbackServletContextListener</listener-class>
</listener>
然后是所有其他听众。
不知道log4j,但我认为有类似的东西...
No idea about log4j, but I think there's something similar...
编辑:是的,有:
<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
来源: https://logging.apache.org/log4j/2.x/manual/webapp.html
这篇关于从servlet上下文记录销毁事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!