If one is not running inside a web application, what is the proper way to shutdown Log4j2? I only see a noop LogManager.shutdown()
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Explanation and story behind
What Log4j 2 use internally is what they call a ShutdownRegistrationStrategy. It consist of a class that register one or many shutdown callback(s) that would be called at the appropriate time. They provide interface named
ShutdownCallbackRegistry
and their default implementation namedDefaultShutdownCallbackRegistry
. This implementation register itself as a shutdown hook to the JVM, withRuntime.getRuntime().addShutdownHook()
.According to some issues on the Log4j 2 bug tracker (LOG4J2-868 and LOG4J2-658), the right way would be to provide your own implementation of the
ShutdownCallbackRegistry
interface. Since there is no public API to shutdown Log4j 2, this is the only real and valid way to archive this.Proposed solution
So, what I did to fix this is that I implemented my own version of the
ShutdownCallbackRegistry
interface. It mostly does the same things the default implementation does, but instead of registering itself as a shutdown hook, it wait until it's invoked manually. That is made possible internally by keeping the instance of the object in a static field. You then only have to call single static method to launch the shutdown procedure, hence why I named it theStaticShutdownCallbackRegistry
.You can find the complete solution and instructions on GitHub/DjDCH/Log4j-StaticShutdown and use it in you own projects. Basically, at the end, you only have to do something like this in your application:
I can't say without any doubt that this is the perfect solution and that my implementation is perfect, but I tried to do it the right way (according to the none-existent documentation about it). I'll be glad to hear feedback from you, either if you find this solution appropriate or not.
Log4j-Web library is the preferred way to properly clean-up resources in Web-application.
To enable it, add such a dependency to your pom.xml
It will work 'out-of-the box' in Servlet 3.* environment. If you're using older Servlet specification, details how to enable proper cleaning for log4j2 are available here: https://logging.apache.org/log4j/2.x/manual/webapp.html
There is no public API for this, but you can use
If you have an interest in having a public API for this, please vote for or add a comment here: https://issues.apache.org/jira/browse/LOG4J2-124
Update:
In Log4j 2.5 you can call
Configurator.shutdown()
. With Log4j 2.6 you can callLogManager.shutdown()
.