Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j configure and watch not working properly

Tags:

java

log4j

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.

like image 850
M.J. Avatar asked Sep 19 '11 09:09

M.J.


2 Answers

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

like image 80
cadrian Avatar answered Oct 04 '22 13:10

cadrian


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!

like image 31
edumar Avatar answered Oct 04 '22 15:10

edumar