Within my Servlet-based application, I would like to log events for startup and shutdown.
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.");
}
}
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?