java 我如何正确使用 Log4j,关闭所有 Appender,因此关闭文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/839255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I properly Log4j, closing all Appenders and, therefore, files
提问by alamar
My servlet-based web-app sometimes fails to close Log4j log files when redeployed, leaking to file descriptor leakage and occassionally leading to servlet-contained dying from 'too many open files'.
我的基于 servlet 的 web 应用程序在重新部署时有时无法关闭 Log4j 日志文件,泄漏到文件描述符泄漏,有时会导致包含 servlet 的“打开的文件太多”死亡。
I have a ContextListener, what should I put into its contextDestroyed() to tell log4j to shut down and release all resources?
我有一个 ContextListener,我应该在它的 contextDestroyed() 中放入什么来告诉 log4j 关闭并释放所有资源?
Quick skim over javadocs revealed Hierachery class with shutdown() method. I have no idea on how to actually get the current Hierachery, and the javadoc states this class has no user-serviceable components inside :)
快速浏览 javadoc 揭示了带有 shutdown() 方法的 Hierachery 类。我不知道如何实际获取当前的层次结构,并且 javadoc 声明此类内部没有用户可服务的组件:)
回答by kgiannakakis
Try this:
试试这个:
org.apache.log4j.LogManager.shutdown();
However, the problem you are experiencing is strange and shouldn't be happening. What JVM, container, log4j version are you using?
但是,您遇到的问题很奇怪,不应该发生。您使用的是什么 JVM、容器、log4j 版本?
回答by Ulrik
I had the same problem but my solution required two steps:
我遇到了同样的问题,但我的解决方案需要两个步骤:
First, I had to call LogManager.shutdown()from a servlets destroy()method.
首先,我必须LogManager.shutdown()从 servletsdestroy()方法调用。
Then I had to fix the close()method of our own Appender implementation (which is a subclass of AppenderSkeleton), so that it properly closes the appenders from getAllAppenders().
然后我不得不修复close()我们自己的 Appender 实现的方法(它是 的子类AppenderSkeleton),以便它正确地关闭getAllAppenders().

