java - log4j2 Unable to register shutdown hook because JVM is shutting down -


i'm trying use log4j2 in tomcat based web application, added log4j web module along other essential jars. when stopping web application i'm getting following exception.

fatal unable register shutdown hook because jvm shutting down 

why i'm getting error , can prevent error ?

thanks!

as explained in pouriya's answer, trying use log4j2 when application stopping without proper shutdown hook. since talking tomcat web application, assume using servlets. (if don't, see second part below). then, must declare in contextlistener:

public class contextlistener implements servletcontextlistener {     @override     public void contextinitialized(servletcontextevent evt) {         string appendername = evt.getservletcontext().getinitparameter("log4jcontextname");         file file = new file(evt.getservletcontext().getinitparameter("log4jconfiguration"));         if(!file.exists()){             logger = logmanager.getrootlogger();         } else{             logger = logmanager.getlogger(appendername);         }     } }  @override public void contextdestroyed(servletcontextevent evt) {     // don't need here, logger.     // log4j2 jar handle properly. } 

as usual web applications, log4j2's config file must indicated in web.xml file, as:

<?xml version="1.0" encoding="utf-8"?> <web-app ... >     <context-param>         <param-name>log4jcontextname</param-name>         <param-value>myapplication</param-value>     </context-param>      <context-param>         <param-name>log4jconfiguration</param-name>         <param-value>/the/path/to/your/log4j.properties.file</param-value>     </context-param> </web-app> 

if instead using normal application, need add explicitly shutdown hook:

public static void main( string[] args ) {      ...      file file = new file(logfilename);     if(!file.exists()){         logger = logmanager.getrootlogger();     } else {         system.setproperty("log4j.configurationfile", file.touri().tourl().tostring());         logger = logmanager.getlogger("yourprogramname");     }      runtime.getruntime().addshutdownhook(new thread() {         public void run() {             logger.info("shutting down - closing application");             // shut down (e.g. threads) need to.              // shut down log4j             if( logmanager.getcontext() instanceof loggercontext ) {                 logger.debug("shutting down log4j2");                 configurator.shutdown((loggercontext)logmanager.getcontext());             } else                 logger.warn("unable shutdown log4j2");              // logger not usable anymore             system.out.println("done.");         }     }); } 

the shutdown hook called when application ended (i.e. when calling system.exit(0).

you need add in log4j2 config file:

xml version:

<?xml version="1.0" encoding="utf-8"?> <configuration shutdownhook="disable"> ... </configuration> 

json version:

{"configuration":     {         "shutdownhook":"disable",         ....     } } 

this tell log4j2 you're handling shutdown of logger yourself.

(since in normal application don't have web.xml file, have retrieve configuration file in other way).


Comments