ppkarwasz commented on issue #2007:
URL: 
https://github.com/apache/logging-log4j2/issues/2007#issuecomment-1827683725

   @PavelTurk,
    
   > What you suggest is not a solution for me. I need to use my own 
shutdownHook in my application. This shutdownHook must do some logic to close 
application correctly AND only in the very end I can shutdown `log4j2`.
   
   What about something like this:
   
   ```java
       public static void main(String[] args) {
           logger.info("Hello JUL world!");
           Runtime.getRuntime().addShutdownHook(new Thread() {
               @Override
               public void run() {
                   // Do some shutdown stuff
                   // ...
                   // Stop the logging backend.
                   LogManager.shutdown();
               }
           });
           System.exit(0);
       }
   ```
   
   > I still think that this is a bug in `log4j2`. It doesn't matter what java 
version is used - there is always possibility that JRE will log some messages 
after `LogManager.shutdown()`, so `log4j2` should support such cases.
   
   This issue is very similar to #1782: as explained in the Javadoc you cited, 
there is no way to influence the order of execution of JVM shutdown hooks. We 
can only **document** this fact and explain that `LogManager#shutdown()` is a 
method for advanced users and should:
   
   1. only be called in a shutdown hook or equivalent (e.g. 
`ServletContextListener` for web applications),
   2. the other shutdown hooks should not use logging (e.g. they can use 
`System.err`) or the system must guarantee that the shutdown hook is executed 
**last**.
   
   AFAIK the only way for Log4j JUL to detect that the application is shutting 
down is to use `Runtime#addShutdownHook`, but this works only **after** 
`Shutdown#runHooks` has been invoked. The `Shutdown#logRuntimeExit` method that 
causes the reinitialisation problem in your application **happens before** the 
`Shudown#runHooks` call.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to