11

It's possible to disable shutdown hooks in log4j2 via configuration:

<Configuration shutdownHook="disable">

Is it possible to do so programmatically?

Justin Wong
  • 1,455
  • 3
  • 18
  • 25
  • The link tells me how to disable shutdown hooks via configuration, and shutdown log4j2 manually in code. It doesn't tell me how to disable shutdown hooks in code. – Justin Wong Jun 05 '15 at 06:28

1 Answers1

7

I know, it's probably outdated but I felt on your question and I was in the same situation. So for people interested, I use this piece of code to stop the shutdown hook programmaticaly :

final LoggerContextFactory factory = LogManager.getFactory();

if (factory instanceof Log4jContextFactory) {
    LOG.info("register shutdown hook");
    Log4jContextFactory contextFactory = (Log4jContextFactory) factory;

    ((DefaultShutdownCallbackRegistry) contextFactory.getShutdownCallbackRegistry()).stop();
}

and in my own shutdown hook

LogManager.shutdown();

log4j2: 2.8.2 (but should be available since 2.6)

Laurent P.
  • 116
  • 1
  • 5