Java – no log output on contextdestroyed using servletcontextlistener and slf4j
I'm trying to write a message to the logger (vaadin) servlet has stopped, which uses slf4j and log4j2
For this purpose, I use servletcontextlistener, which logs messages when the application starts But when recording in the contextdestroyed method, I can't get any output... This 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 guess this may be because logging is no longer possible when contextdestroyed is called because they have been destroyed by the garbage collector
So now my question is, can I record that the servlet has stopped before the context is destroyed, or can I have the contextlistener execute before the log4j2 logger is destroyed?
Thank you in advance!
Solution
First, do you have log4j 2. 0 in your classpath X API, core and web jar? log4j-web-2. x. Jar also registers a context listener to turn off the logging subsystem when uninstalling web applications
If your listener runs after this, you will no longer be able to record anything
You can set < configuration status = "trace" To check what happened
