I am trying to write a message to the logger that a (Vaadin) servlet has stopped, this using SLF4J and Log4j2.
For this I am using a ServletContextListener
which logs a message when the application has started. However I have been unable to get any output when logging inside the contextDestroyed
method... Here is my implementation:
@WebListener
public class VaadinLogger implements ServletContextListener {
private static final Logger logger = LoggerFactory.getLogger(VaadinLogger.class);
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
// Remove appenders from JUL loggers
SLF4JBridgeHandler.removeHandlersForRootLogger();
// Install bridge
SLF4JBridgeHandler.install();
// Get servlet context
ServletContext context = contextEvent.getServletContext();
// Retrieve name
String name = context.getServletContextName();
// Log servlet init information
logger.info("Start \"{}\"", name);
}
@Override
public void contextDestroyed(ServletContextEvent contextEvent) {
// Get servlet context
ServletContext context = contextEvent.getServletContext();
// Retrieve name
String name = context.getServletContextName();
// Log servlet destroy information
logger.info("End \"{}\"{}", name, System.lineSeparator()));
// Uninstall bridge
SLF4JBridgeHandler.uninstall();
}
}
At this point, I'm guessing this is probably because at the point contextDestroyed
is called, logging is no longer possible because they have already been destroyed by the garbage collector.
So now my question is, is it possible to either log that the servlet has stopped before the context is destroyed, or make the contextlistener execute before log4j2 loggers are destroyed?
Thanks in advance!
ServletContext
has actually been destroyed? What is causing its destruction? – CrotchetSLF4JBridgeHandler.uninstall();
fixed some errors in my console output.. Also when I changelogger.info(...
toSystem.out.println(...
the message is printed to console. – Kimes