I have a specialized logger class which uses the java.util.logging.Logger class. I want to be able to use this logger in the shutdown hook of another class. However, it doesn’t seem to log at shutdown. From what I’ve read, there may already be a shutdown hook activated for the logger itself which is causing the issue.
How can I get this to work? Ideally, I would like it to be seen in the log file that I did in fact execute the shutdown hook when the process terminated.
Again looking at the source, the solution appears to be to define a system property
java.util.logging.managerwhich is a subclass of LogManager which overrides thereset();method so the Loggers continue to work on shutdown.prints
From this code for LogManager, you can see see there is a shutdown hook which dismantles the handlers and closes them. Logger only works in shutdown if it hasn’t been used before so this code is not run.
From my own testing
prints
but if you add
outside this block you get nothing.