Shutting down Log4j2 in a JavaEE environment that does not include servlets

Schindler, RenĂ© <[email protected]> Fri, 10 Jun 2022 14:36:39 +0000
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <SA0P114MB1306995AE4378A6F7CD201F5CAA69@SA0P114MB1306.NAMP114.PROD.OUTLOOK.COM>
Hello,

I have a problem with shutting down log4j2 in a Java EE environment, which does not include servlets.
Currently my code for shutting down log4j2 is located in a Singleton Startup Bean in a @PreDestroy annotated method.


@PreDestroy
public static void shutDown() {
        LogManager.shutdown();
}

This did not work however. It looks like the log4j is shutting down only to be started up again.
This restart prevents our application from undeploying, leaving all the loaded classes on the server.
My current assumption is  the restart happens,  because of something in our application is calling LogManager.getLogger(clazz) after LogManager.shutdown() is called.

Therefore we came up with this workaround:

@PreDestroy
public static void shutDown() {
    org.apache.logging.log4j.LogManager.shutdown();

    Configurator.reconfigure(new NullConfiguration());
    Server.unregisterMBeans();
}

It prevents the instantiation of the appenders and async loggers and replacing the current config with a NullConfiguration.
The restart still takes place, but it does not prevent the undeplyoment any more (when getting rid of existing MBeans).

Is there a more elegant solution, maybe getting rid of the restart in the first place?

Best regards

Rene Schindler


________________________________

This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited.