Log4j的配置和观察不能正常工作[英] Log4j configure and watch not working properly

本文是小编为大家收集整理的关于Log4j的配置和观察不能正常工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在应用程序中使用log4j作为loggin目的.从现在开始配置日志记录,我正在使用以下代码:

LogManager.resetConfiguration();
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties"); 
Properties props= new Properties();
props.load(stream);
PropertyConfigurator.configure(props);

但问题是,每当我想在此过程中更改日志记录级别时,我都必须重新启动服务器.因此,我将代码更改为: -

LogManager.resetConfiguration();
PropertyConfigurator.configureAndWatch(("log4j.properties", 900000L);

理想情况下,此代码应有助于在指定时间后重新加载log4j.properties文件,我提到的时间为15分钟.但是,该代码仍然不起作用

我在代码期间错过了什么吗?

问候.

推荐答案

configureAndWatch()观看文件.不是集体路径中的资源.

其他推荐答案

我尝试了解决方案并正常工作!关键是您必须像文件一样提供路径.

//Resource
DOMConfigurator.configureAndWatch("/log4j.xml", 2000L);

//File
DOMConfigurator.configureAndWatch("./src/log4j.xml", 2000L);

尝试第二个选项并修改 log4j.xml 并测试它!

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

问题描述

I am using log4j for loggin purpose in my application. Since now to configure the logging i was using the following code :

LogManager.resetConfiguration();
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties"); 
Properties props= new Properties();
props.load(stream);
PropertyConfigurator.configure(props);

But the problem with this was , that whenever i wanted to change the logging level during the process, i had to restart the server. So i changed the code to :-

LogManager.resetConfiguration();
PropertyConfigurator.configureAndWatch(("log4j.properties", 900000L);

this code ideally should help to re-load the log4j.properties file after the time specified, which i have mentioned as 15 minutes. But still the code is not working

Am i missing somthing during the code?

Regards.

推荐答案

configureAndWatch() watches files. Not resources in the classpath.

其他推荐答案

I tried the solution and works fine! The point is that your must provide the path like a file not like a resource.

//Resource
DOMConfigurator.configureAndWatch("/log4j.xml", 2000L);

//File
DOMConfigurator.configureAndWatch("./src/log4j.xml", 2000L);

Try the second option and modify the log4j.xml and test it!