问题描述
问题是,每当您更改log4j.properties/log4j.xml时,您都需要重新启动tomcat [或说任何其他服务器].是否有重新加载log4j配置的解决方法?
推荐答案
有没有办法将log4j获取 自动重新加载配置 文件如果更改?
是. domconfigurator和 PropertyConfigurator支持自动 重新加载 configureandwatch方法.请参阅其他推荐答案
从log4j 2.x开始,您可以定期重新加载配置,在此示例中每30秒:
<configuration monitorInterval="30">请看一下其他推荐答案
您可以使用以下简短步骤编写一些初始代码:
- 听" treftre_start_event",
- 当事件发生时(每次cat重新启动一次),请使用configureandwatch方法启动log4j
- 也不要忘记安装关闭钩以清理观察者线程
请参阅此博客文章以获取详细信息 - github
.
问题描述
The problem is, whenever you change the log4j.properties/log4j.xml, you need to restart the tomcat [ or say any other server ]. Is there any workaround of reloading the log4j configuration?
推荐答案
From http://logging.apache.org/log4j/1.2/faq.html#3.6
Is there a way to get log4j to automatically reload a configuration file if it changes?
Yes. Both the DOMConfigurator and the PropertyConfigurator support automatic reloading through the configureAndWatch method. See the API documentation for more details.
Because the configureAndWatch launches a separate wathdog thread, and because there is no way to stop this thread in log4j 1.2, the configureAndWatch method is unsafe for use in J2EE envrironments where applications are recycled.
Said that, I've successfully used PropertyConfigurator#configureAndWatch method in a Java EE environment (Sun One Web Server, not Tomcat).
其他推荐答案
As of log4j 2.x you can reload the config periodically, in this example every 30 seconds:
<configuration monitorInterval="30">
Please take a look here for more information on log4j 2.x configuration:
其他推荐答案
You can write a little initializer code with the following short steps:
- listen for the "BEFORE_START_EVENT",
- when the event happens (once per Tomcat restart), start log4j using the configureAndWatch method
- also don't forget to install a shutdown hook to cleanup the watcher thread
See this blog post for details - reload log4j configuration in tomcat
They also moved it to github.