java - How to log within shutdown hooks with Log4j2? -
log4j2 uses shutdown hooks end it's services. of course want log throughout whole lifecycle of application - shutdown included. log4j no problem. seems impossible. logging shuts down, while application still working on it. has hope me?
best regards martin
as of 2.0-beta9 configurable in xml
<configuration ... shutdownhook="disable">
considering disabled, guess need manually shutdown logging system @ end of shutdown hook. couldn't find means thorough external interface, in internal api
import org.apache.logging.log4j.logmanager; import org.apache.logging.log4j.core.config.configurator; import org.apache.logging.log4j.core.loggercontext; ... public static void main(string[] args) { final annotationconfigapplicationcontext springcontext = new annotationconfigapplicationcontext(appconfig.class) runtime.getruntime().addshutdownhook(new thread() { public void run() { //shutdown application log.info("shutting down spring context"); springcontext.close(); //shutdown log4j2 if( logmanager.getcontext() instanceof loggercontext ) { logger.info("shutting down log4j2"); configurator.shutdown((loggercontext)logmanager.getcontext()); } else logger.warn("unable shutdown log4j2"); } }); //more application initialization }
Comments
Post a Comment