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.