如何正确关闭log4j2[英] How to properly shutdown log4j2

本文是小编为大家收集整理的关于如何正确关闭log4j2的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如果一个人没有在Web应用程序中运行,则关闭Log4J2的正确方法是什么?我只看到一个noop logmanager.shutdown()

推荐答案

没有公共API,但是您可以使用

((LifeCycle) LogManager.getContext()).stop();

如果您有兴趣为此提供公共API,请在此处投票或添加评论: https://issues.apache.org/jira/browse/log4j2-124


更新:

在log4j 2.5中,您可以调用Configurator.shutdown().使用log4j 2.6您可以调用LogManager.shutdown().

其他推荐答案

背后的解释和故事

log4j 2在内部使用的是他们所说的 shutdownRegistrateStrategy .它由注册一个或多个关闭回调的类组成,该回调将在适当的时间调用.他们提供名为 DefaultShutdownCallbackRegistry .此实现将本身注册为JVM的关闭挂钩,并使用Runtime.getRuntime().addShutdownHook().

根据log4j 2 bug跟踪器( log4j2-868 )和 log4j2-658 ),正确的方法是提供您自己的实施ShutdownCallbackRegistry接口.由于没有公共API可以关闭Log4J 2,因此这是归档此内容的唯一真实且有效的方法.

建议的解决方案

所以,我为解决这个问题所做的是我实现了自己的ShutdownCallbackRegistry接口的版本.它主要执行默认实现所做的相同的操作,但是它没有将自己注册为关闭挂钩,而是等到手动调用它.通过将对象的实例保存在静态字段中,这是在内部内部实现的.然后,您只需要调用单个静态方法来启动关闭过程,因此为什么我将其命名为 StaticShutdownCallbackRegistry .

您可以在 github/djdch/djdch/log4j-staticshutdown上找到完整的解决方案和说明它在您自己的项目中.基本上,最后,您只需要在应用程序中做类似的事情:

StaticShutdownCallbackRegistry.invoke();

我不能毫无疑问地说这是完美的解决方案,并且我的实现是完美的,但是我试图以正确的方式(根据对此的无存在的文档)进行操作.如果您觉得此解决方案合适,我会很高兴听到您的反馈.

其他推荐答案

log4j-web 库是正确清理Web申请资源的首选方法.

要启用它,请在您的pom.xml

中添加这样的依赖项
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>${log4j2.version}</version>
    <scope>runtime</scope>
</dependency>

它将在servlet 3中工作.*环境. 如果您使用的是较旧的servlet规范,则可以在此处提供详细信息如何启用log4j2的正确清洁: https://logging.apache.org/log/log4j/2.x/manual/webapp.html

本文地址:https://www.itbaoku.cn/post/1574756.html

问题描述

If one is not running inside a web application, what is the proper way to shutdown Log4j2? I only see a noop LogManager.shutdown()

推荐答案

There is no public API for this, but you can use

((LifeCycle) LogManager.getContext()).stop();

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 call LogManager.shutdown().

其他推荐答案

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 named DefaultShutdownCallbackRegistry. This implementation register itself as a shutdown hook to the JVM, with Runtime.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 the StaticShutdownCallbackRegistry.

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:

StaticShutdownCallbackRegistry.invoke();

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

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>${log4j2.version}</version>
    <scope>runtime</scope>
</dependency>

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