I had a peculiar problem this morning. log4j logging output was being turned off in a call to a third party library1. Not very much fun, particularly when exceptions start going unlogged.
Here are two code snippets that were useful for debugging the problem. This code resets the log4j configuration from the contents of the log4j.properties file:
static void resetLog4j() {
LogManager.resetConfiguration();
ClassLoader cl = this.getClass().getClassLoader();
URL log4jprops = cl.getResource(
"log4j.properties");
if (log4jprops != null) {
(new PropertyConfigurator())
.configure(log4jprops);
}
}
The log4j config can be dumped to standard out like this:
private void printLog4jConfig() {
System.out.println("#log4j Config");
PrintWriter pw = new PrintWriter(System.out);
PropertyPrinter pp = new PropertyPrinter(pw);
pp.print(pw);
System.out.println("#End of Config");
}
1 Google turned up a solution to the same problem in a previous version of the product, but nothing for the current version. I couldn’t find any other mention in the Knowledge Base or Support forums. I submitted a bug report, but meanwhile I have a workaround.
Comments
This entry is currently the number 1 hit for "log4j.properties". ;-)
you are an awesome geek!
i wanted to know about cardboard, i guess this isnt the place to find it. but thanks anyways i learned alot about your problems in the morning. dont ask me why im looking for cardboard! school project. i dont think ill find it. i think im going to talk to the teacher about his stupid assignment. anyways thats my problem of the morning
i needed the properties of cardboard but this ain't it so c ya u computer geeks good luck be in a loser!!!!!!!!!!!!!!!!!!!!!!!
peace
holl@ back
Why are all of you so sad
nice blog. keep it up. visit us also at<a href="www.yahoo.com">john</a>
boooooring!!!!!
This worked great for me. It reloads the log4j.properties file when a execute the code. That is all I needed.
If I change the log4j.properties, will log4j reload it automatically?