I'm using Spring, and on termination I'm having @PreDestroy clean up beans. I don't understand why logging randomly succeeds sometimes, and fails on others.
// Using Log4j2
Logger log = LogManager.getLogger(MyClass.class);
@PreDestroy
public void close() {
log.warn("Test");
}
Sometimes I will get nothing (no "Test" is logged), other times I will get:
[13:48:44] INFO MyClass: Test
If I include System.out.println("Is this run?");
in the close() method, it will always print.
I'm not actually sure what is happening. I don't know if it's because the JVM is shutting down and the logger is killed... but I thought that would throw some kind of exception?
Note that the logging logs both to a file + stdout, I don't know if that would affect anything. Logging works fine for the other countless thousands of lines of code, but not this.
NOTE: I am open to switching logging libraries if it ends up being this particular library.
EDIT: MyClass would be a bean in a spring.xml document.
Exactly same as @Devon_C_Miller answer but updated to reflect latest version of log4j2
and in the log4j2 configuration
I dont have enough reputation to add a comment, so i will answer here:
According to the JavaEE API javadoc:
Check if an exception is being thrown somewhere else "silently", that might be one cause.
I think it comes down to this, from Runtime.addShutdownHook:
So, as long as LogManager and the Spring IOC container are both shut down by JVM shutdown hooks, there is no way to ensure the message will be recorded. If the LogManager is shut down first, the message is lost. If the IOC container is shut down first, the message is recorded.
If you are running in a JEE container, there's probably little you can do to change this.
However, if you're running in a stand-alone environment, you can add a
shutdownHook="disable"
to the Log4j 2<configuration>
tag. This prevents Log4j 2 from registering it's own shutdown hook. Then, instead of callingctx.registerShutdownHook()
(the recommended way to shutdown the IOC), you register your own shutdown hook. Something like:Update: Corrected process of shutting down Log4j2.
Caveat: I am away from my usual build machines, so I have not compiled this, but I believe it's hitting the correct APIs.