Provide a ShutdownRegistrationStrategy for Log4j 2 designed to be manually invoked.
- Java and Maven installed
Add the dependency in your pom.xml
file:
<dependency>
<groupId>com.djdch.log4j</groupId>
<artifactId>log4j-staticshutdown</artifactId>
<version>1.1.0</version>
</dependency>
Add this static initialization block in your Main class:
static {
System.setProperty("log4j.shutdownCallbackRegistry", "com.djdch.log4j.StaticShutdownCallbackRegistry");
}
WARNING! Even if this works, there is no warranty that it always will, since there is no way to ensure that your Main class will be loaded before Log4j (and it won't work if that doesn't happen). To be safe, you should instead set the system property from the command line when launching your jar.
Then, you can invoke manually the StaticShutdownCallbackRegistry
in your shutdown procedure:
StaticShutdownCallbackRegistry.invoke();