Programmatically disabling shutdown hook in log4j 2
Asked Answered
A

1

11

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

<Configuration shutdownHook="disable">

Is it possible to do so programmatically?

Androgen answered 5/6, 2015 at 2:52 Comment(1)
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.Androgen
D
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)

Declan answered 2/1, 2018 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.